home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / elfcode.h < prev    next >
C/C++ Source or Header  |  1994-10-21  |  195KB  |  6,553 lines

  1. /* ELF executable support for BFD.
  2.    Copyright 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4.    Written by Fred Fish @ Cygnus Support, from information published
  5.    in "UNIX System V Release 4, Programmers Guide: ANSI C and
  6.    Programming Support Tools".  Sufficient support for gdb.
  7.  
  8.    Rewritten by Mark Eichin @ Cygnus Support, from information
  9.    published in "System V Application Binary Interface", chapters 4
  10.    and 5, as well as the various "Processor Supplement" documents
  11.    derived from it. Added support for assembler and other object file
  12.    utilities.  Further work done by Ken Raeburn (Cygnus Support), Michael
  13.    Meissner (Open Software Foundation), and Peter Hoogenboom (University
  14.    of Utah) to finish and extend this.
  15.  
  16. This file is part of BFD, the Binary File Descriptor library.
  17.  
  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2 of the License, or
  21. (at your option) any later version.
  22.  
  23. This program is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. GNU General Public License for more details.
  27.  
  28. You should have received a copy of the GNU General Public License
  29. along with this program; if not, write to the Free Software
  30. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  31.  
  32. /* Problems and other issues to resolve.
  33.  
  34.    (1)    BFD expects there to be some fixed number of "sections" in
  35.         the object file.  I.E. there is a "section_count" variable in the
  36.     bfd structure which contains the number of sections.  However, ELF
  37.     supports multiple "views" of a file.  In particular, with current
  38.     implementations, executable files typically have two tables, a
  39.     program header table and a section header table, both of which
  40.     partition the executable.
  41.  
  42.     In ELF-speak, the "linking view" of the file uses the section header
  43.     table to access "sections" within the file, and the "execution view"
  44.     uses the program header table to access "segments" within the file.
  45.     "Segments" typically may contain all the data from one or more
  46.     "sections".
  47.  
  48.     Note that the section header table is optional in ELF executables,
  49.     but it is this information that is most useful to gdb.  If the
  50.     section header table is missing, then gdb should probably try
  51.     to make do with the program header table.  (FIXME)
  52.  
  53.    (2)  The code in this file is compiled twice, once in 32-bit mode and
  54.     once in 64-bit mode.  More of it should be made size-independent
  55.     and moved into elf.c.
  56.  
  57.    (3)    ELF section symbols are handled rather sloppily now.  This should
  58.     be cleaned up, and ELF section symbols reconciled with BFD section
  59.     symbols.
  60.  
  61.    (4)  We need a published spec for 64-bit ELF.  We've got some stuff here
  62.     that we're using for SPARC V9 64-bit chips, but don't assume that
  63.     it's cast in stone.
  64.  */
  65.  
  66. #include <string.h>        /* For strrchr and friends */
  67. #include "bfd.h"
  68. #include "sysdep.h"
  69. #include "bfdlink.h"
  70. #include "libbfd.h"
  71. #include "libelf.h"
  72.  
  73. /* Renaming structures, typedefs, macros and functions to be size-specific.  */
  74. #define Elf_External_Ehdr    NAME(Elf,External_Ehdr)
  75. #define Elf_External_Sym    NAME(Elf,External_Sym)
  76. #define Elf_External_Shdr    NAME(Elf,External_Shdr)
  77. #define Elf_External_Phdr    NAME(Elf,External_Phdr)
  78. #define Elf_External_Rel    NAME(Elf,External_Rel)
  79. #define Elf_External_Rela    NAME(Elf,External_Rela)
  80. #define Elf_External_Dyn    NAME(Elf,External_Dyn)
  81.  
  82. #define elf_core_file_failing_command    NAME(bfd_elf,core_file_failing_command)
  83. #define elf_core_file_failing_signal    NAME(bfd_elf,core_file_failing_signal)
  84. #define elf_core_file_matches_executable_p \
  85.   NAME(bfd_elf,core_file_matches_executable_p)
  86. #define elf_object_p            NAME(bfd_elf,object_p)
  87. #define elf_core_file_p            NAME(bfd_elf,core_file_p)
  88. #define elf_get_symtab_upper_bound    NAME(bfd_elf,get_symtab_upper_bound)
  89. #define elf_get_dynamic_symtab_upper_bound \
  90.   NAME(bfd_elf,get_dynamic_symtab_upper_bound)
  91. #define elf_swap_reloc_in        NAME(bfd_elf,swap_reloc_in)
  92. #define elf_swap_reloca_in        NAME(bfd_elf,swap_reloca_in)
  93. #define elf_swap_reloc_out        NAME(bfd_elf,swap_reloc_out)
  94. #define elf_swap_reloca_out        NAME(bfd_elf,swap_reloca_out)
  95. #define elf_swap_symbol_in        NAME(bfd_elf,swap_symbol_in)
  96. #define elf_swap_symbol_out        NAME(bfd_elf,swap_symbol_out)
  97. #define elf_swap_dyn_in            NAME(bfd_elf,swap_dyn_in)
  98. #define elf_swap_dyn_out        NAME(bfd_elf,swap_dyn_out)
  99. #define elf_get_reloc_upper_bound    NAME(bfd_elf,get_reloc_upper_bound)
  100. #define elf_canonicalize_reloc        NAME(bfd_elf,canonicalize_reloc)
  101. #define elf_get_symtab            NAME(bfd_elf,get_symtab)
  102. #define elf_canonicalize_dynamic_symtab \
  103.   NAME(bfd_elf,canonicalize_dynamic_symtab)
  104. #define elf_make_empty_symbol        NAME(bfd_elf,make_empty_symbol)
  105. #define elf_get_symbol_info        NAME(bfd_elf,get_symbol_info)
  106. #define elf_get_lineno            NAME(bfd_elf,get_lineno)
  107. #define elf_set_arch_mach        NAME(bfd_elf,set_arch_mach)
  108. #define elf_find_nearest_line        NAME(bfd_elf,find_nearest_line)
  109. #define elf_sizeof_headers        NAME(bfd_elf,sizeof_headers)
  110. #define elf_set_section_contents    NAME(bfd_elf,set_section_contents)
  111. #define elf_no_info_to_howto        NAME(bfd_elf,no_info_to_howto)
  112. #define elf_no_info_to_howto_rel    NAME(bfd_elf,no_info_to_howto_rel)
  113. #define elf_new_section_hook        NAME(bfd_elf,new_section_hook)
  114. #define write_relocs            NAME(bfd_elf,_write_relocs)
  115. #define elf_find_section        NAME(bfd_elf,find_section)
  116. #define elf_bfd_link_add_symbols    NAME(bfd_elf,bfd_link_add_symbols)
  117. #define elf_add_dynamic_entry        NAME(bfd_elf,add_dynamic_entry)
  118. #define elf_link_create_dynamic_sections \
  119.   NAME(bfd_elf,link_create_dynamic_sections)
  120. #define elf_link_record_dynamic_symbol  \
  121.   NAME(bfd_elf,link_record_dynamic_symbol)
  122. #define elf_bfd_final_link        NAME(bfd_elf,bfd_final_link)
  123.  
  124. #if ARCH_SIZE == 64
  125. #define ELF_R_INFO(X,Y)    ELF64_R_INFO(X,Y)
  126. #define ELF_R_SYM(X)    ELF64_R_SYM(X)
  127. #define ELF_R_TYPE(X)    ELF64_R_TYPE(X)
  128. #define ELFCLASS    ELFCLASS64
  129. #define FILE_ALIGN    8
  130. #define LOG_FILE_ALIGN    3
  131. #endif
  132. #if ARCH_SIZE == 32
  133. #define ELF_R_INFO(X,Y)    ELF32_R_INFO(X,Y)
  134. #define ELF_R_SYM(X)    ELF32_R_SYM(X)
  135. #define ELF_R_TYPE(X)    ELF32_R_TYPE(X)
  136. #define ELFCLASS    ELFCLASS32
  137. #define FILE_ALIGN    4
  138. #define LOG_FILE_ALIGN    2
  139. #endif
  140.  
  141. /* Forward declarations of static functions */
  142.  
  143. static unsigned long bfd_add_to_strtab
  144.   PARAMS ((bfd *, struct strtab *, const char *));
  145. static asection *section_from_elf_index PARAMS ((bfd *, unsigned int));
  146.  
  147. static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
  148.  
  149. static long elf_slurp_symbol_table PARAMS ((bfd *, asymbol **, boolean));
  150.  
  151. static boolean elf_slurp_reloc_table PARAMS ((bfd *, asection *, asymbol **));
  152.  
  153. static int elf_symbol_from_bfd_symbol PARAMS ((bfd *,
  154.                          struct symbol_cache_entry **));
  155.  
  156. static boolean elf_compute_section_file_positions
  157.   PARAMS ((bfd *, struct bfd_link_info *));
  158. static boolean prep_headers PARAMS ((bfd *));
  159. static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
  160. static boolean assign_section_numbers PARAMS ((bfd *));
  161. static file_ptr align_file_position PARAMS ((file_ptr));
  162. static file_ptr assign_file_position_for_section
  163.   PARAMS ((Elf_Internal_Shdr *, file_ptr, boolean));
  164. static boolean assign_file_positions_except_relocs PARAMS ((bfd *, boolean));
  165. static void assign_file_positions_for_relocs PARAMS ((bfd *));
  166. static bfd_size_type get_program_header_size PARAMS ((bfd *));
  167. static file_ptr map_program_segments
  168.   PARAMS ((bfd *, file_ptr, Elf_Internal_Shdr *, bfd_size_type));
  169.  
  170. static boolean elf_map_symbols PARAMS ((bfd *));
  171. static boolean swap_out_syms PARAMS ((bfd *));
  172.  
  173. static boolean bfd_section_from_shdr PARAMS ((bfd *, unsigned int shindex));
  174.  
  175. #ifdef DEBUG
  176. static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *));
  177. static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
  178. #endif
  179.  
  180. #define elf_string_from_elf_strtab(abfd,strindex) \
  181.      elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
  182.  
  183.  
  184. /* Structure swapping routines */
  185.  
  186. /* Should perhaps use put_offset, put_word, etc.  For now, the two versions
  187.    can be handled by explicitly specifying 32 bits or "the long type".  */
  188. #if ARCH_SIZE == 64
  189. #define put_word    bfd_h_put_64
  190. #define get_word    bfd_h_get_64
  191. #endif
  192. #if ARCH_SIZE == 32
  193. #define put_word    bfd_h_put_32
  194. #define get_word    bfd_h_get_32
  195. #endif
  196.  
  197. /* Translate an ELF symbol in external format into an ELF symbol in internal
  198.    format. */
  199.  
  200. void
  201. elf_swap_symbol_in (abfd, src, dst)
  202.      bfd *abfd;
  203.      Elf_External_Sym *src;
  204.      Elf_Internal_Sym *dst;
  205. {
  206.   dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
  207.   dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
  208.   dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
  209.   dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
  210.   dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
  211.   dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
  212. }
  213.  
  214. /* Translate an ELF symbol in internal format into an ELF symbol in external
  215.    format. */
  216.  
  217. void
  218. elf_swap_symbol_out (abfd, src, dst)
  219.      bfd *abfd;
  220.      Elf_Internal_Sym *src;
  221.      Elf_External_Sym *dst;
  222. {
  223.   bfd_h_put_32 (abfd, src->st_name, dst->st_name);
  224.   put_word (abfd, src->st_value, dst->st_value);
  225.   put_word (abfd, src->st_size, dst->st_size);
  226.   bfd_h_put_8 (abfd, src->st_info, dst->st_info);
  227.   bfd_h_put_8 (abfd, src->st_other, dst->st_other);
  228.   bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
  229. }
  230.  
  231.  
  232. /* Translate an ELF file header in external format into an ELF file header in
  233.    internal format. */
  234.  
  235. static void
  236. elf_swap_ehdr_in (abfd, src, dst)
  237.      bfd *abfd;
  238.      Elf_External_Ehdr *src;
  239.      Elf_Internal_Ehdr *dst;
  240. {
  241.   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
  242.   dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);
  243.   dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);
  244.   dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);
  245.   dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
  246.   dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
  247.   dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);
  248.   dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);
  249.   dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);
  250.   dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);
  251.   dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);
  252.   dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);
  253.   dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);
  254.   dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);
  255. }
  256.  
  257. /* Translate an ELF file header in internal format into an ELF file header in
  258.    external format. */
  259.  
  260. static void
  261. elf_swap_ehdr_out (abfd, src, dst)
  262.      bfd *abfd;
  263.      Elf_Internal_Ehdr *src;
  264.      Elf_External_Ehdr *dst;
  265. {
  266.   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
  267.   /* note that all elements of dst are *arrays of unsigned char* already... */
  268.   bfd_h_put_16 (abfd, src->e_type, dst->e_type);
  269.   bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
  270.   bfd_h_put_32 (abfd, src->e_version, dst->e_version);
  271.   put_word (abfd, src->e_entry, dst->e_entry);
  272.   put_word (abfd, src->e_phoff, dst->e_phoff);
  273.   put_word (abfd, src->e_shoff, dst->e_shoff);
  274.   bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
  275.   bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
  276.   bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
  277.   bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
  278.   bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
  279.   bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
  280.   bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
  281. }
  282.  
  283.  
  284. /* Translate an ELF section header table entry in external format into an
  285.    ELF section header table entry in internal format. */
  286.  
  287. static void
  288. elf_swap_shdr_in (abfd, src, dst)
  289.      bfd *abfd;
  290.      Elf_External_Shdr *src;
  291.      Elf_Internal_Shdr *dst;
  292. {
  293.   dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
  294.   dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
  295.   dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);
  296.   dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
  297.   dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
  298.   dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);
  299.   dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
  300.   dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
  301.   dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);
  302.   dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
  303.   /* we haven't done any processing on it yet, so... */
  304.   dst->rawdata = (void *) 0;
  305. }
  306.  
  307. /* Translate an ELF section header table entry in internal format into an
  308.    ELF section header table entry in external format. */
  309.  
  310. static void
  311. elf_swap_shdr_out (abfd, src, dst)
  312.      bfd *abfd;
  313.      Elf_Internal_Shdr *src;
  314.      Elf_External_Shdr *dst;
  315. {
  316.   /* note that all elements of dst are *arrays of unsigned char* already... */
  317.   bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
  318.   bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
  319.   put_word (abfd, src->sh_flags, dst->sh_flags);
  320.   put_word (abfd, src->sh_addr, dst->sh_addr);
  321.   put_word (abfd, src->sh_offset, dst->sh_offset);
  322.   put_word (abfd, src->sh_size, dst->sh_size);
  323.   bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
  324.   bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
  325.   put_word (abfd, src->sh_addralign, dst->sh_addralign);
  326.   put_word (abfd, src->sh_entsize, dst->sh_entsize);
  327. }
  328.  
  329.  
  330. /* Translate an ELF program header table entry in external format into an
  331.    ELF program header table entry in internal format. */
  332.  
  333. static void
  334. elf_swap_phdr_in (abfd, src, dst)
  335.      bfd *abfd;
  336.      Elf_External_Phdr *src;
  337.      Elf_Internal_Phdr *dst;
  338. {
  339.   dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
  340.   dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
  341.   dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
  342.   dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
  343.   dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
  344.   dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
  345.   dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
  346.   dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
  347. }
  348.  
  349. static void
  350. elf_swap_phdr_out (abfd, src, dst)
  351.      bfd *abfd;
  352.      Elf_Internal_Phdr *src;
  353.      Elf_External_Phdr *dst;
  354. {
  355.   /* note that all elements of dst are *arrays of unsigned char* already... */
  356.   bfd_h_put_32 (abfd, src->p_type, dst->p_type);
  357.   put_word (abfd, src->p_offset, dst->p_offset);
  358.   put_word (abfd, src->p_vaddr, dst->p_vaddr);
  359.   put_word (abfd, src->p_paddr, dst->p_paddr);
  360.   put_word (abfd, src->p_filesz, dst->p_filesz);
  361.   put_word (abfd, src->p_memsz, dst->p_memsz);
  362.   bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
  363.   put_word (abfd, src->p_align, dst->p_align);
  364. }
  365.  
  366. /* Translate an ELF reloc from external format to internal format. */
  367. INLINE void
  368. elf_swap_reloc_in (abfd, src, dst)
  369.      bfd *abfd;
  370.      Elf_External_Rel *src;
  371.      Elf_Internal_Rel *dst;
  372. {
  373.   dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
  374.   dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
  375. }
  376.  
  377. INLINE void
  378. elf_swap_reloca_in (abfd, src, dst)
  379.      bfd *abfd;
  380.      Elf_External_Rela *src;
  381.      Elf_Internal_Rela *dst;
  382. {
  383.   dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
  384.   dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
  385.   dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend);
  386. }
  387.  
  388. /* Translate an ELF reloc from internal format to external format. */
  389. INLINE void
  390. elf_swap_reloc_out (abfd, src, dst)
  391.      bfd *abfd;
  392.      Elf_Internal_Rel *src;
  393.      Elf_External_Rel *dst;
  394. {
  395.   put_word (abfd, src->r_offset, dst->r_offset);
  396.   put_word (abfd, src->r_info, dst->r_info);
  397. }
  398.  
  399. INLINE void
  400. elf_swap_reloca_out (abfd, src, dst)
  401.      bfd *abfd;
  402.      Elf_Internal_Rela *src;
  403.      Elf_External_Rela *dst;
  404. {
  405.   put_word (abfd, src->r_offset, dst->r_offset);
  406.   put_word (abfd, src->r_info, dst->r_info);
  407.   put_word (abfd, src->r_addend, dst->r_addend);
  408. }
  409.  
  410. INLINE void
  411. elf_swap_dyn_in (abfd, src, dst)
  412.      bfd *abfd;
  413.      const Elf_External_Dyn *src;
  414.      Elf_Internal_Dyn *dst;
  415. {
  416.   dst->d_tag = get_word (abfd, src->d_tag);
  417.   dst->d_un.d_val = get_word (abfd, src->d_un.d_val);
  418. }
  419.  
  420. INLINE void
  421. elf_swap_dyn_out (abfd, src, dst)
  422.      bfd *abfd;
  423.      const Elf_Internal_Dyn *src;
  424.      Elf_External_Dyn *dst;
  425. {
  426.   put_word (abfd, src->d_tag, dst->d_tag);
  427.   put_word (abfd, src->d_un.d_val, dst->d_un.d_val);
  428. }
  429.  
  430. /* String table creation/manipulation routines */
  431.  
  432. static struct strtab *
  433. bfd_new_strtab (abfd)
  434.      bfd *abfd;
  435. {
  436.   struct strtab *ss;
  437.  
  438.   ss = (struct strtab *) malloc (sizeof (struct strtab));
  439.   if (!ss)
  440.     {
  441.       bfd_set_error (bfd_error_no_memory);
  442.       return NULL;
  443.     }
  444.   ss->tab = malloc (1);
  445.   if (!ss->tab)
  446.     {
  447.       bfd_set_error (bfd_error_no_memory);
  448.       return NULL;
  449.     }
  450.   *ss->tab = 0;
  451.   ss->nentries = 0;
  452.   ss->length = 1;
  453.  
  454.   return ss;
  455. }
  456.  
  457. static unsigned long
  458. bfd_add_to_strtab (abfd, ss, str)
  459.      bfd *abfd;
  460.      struct strtab *ss;
  461.      const char *str;
  462. {
  463.   /* should search first, but for now: */
  464.   /* include the trailing NUL */
  465.   int ln = strlen (str) + 1;
  466.  
  467.   /* FIXME: This is slow.  Also, we could combine this with the a.out
  468.      string table building and use a hash table, although it might not
  469.      be worth it since ELF symbols don't include debugging information
  470.      and thus have much less overlap.  */
  471.   ss->tab = realloc (ss->tab, ss->length + ln);
  472.   if (ss->tab == NULL)
  473.     {
  474.       bfd_set_error (bfd_error_no_memory);
  475.       return (unsigned long) -1;
  476.     }
  477.  
  478.   strcpy (ss->tab + ss->length, str);
  479.   ss->nentries++;
  480.   ss->length += ln;
  481.  
  482.   return ss->length - ln;
  483. }
  484.  
  485. static int
  486. bfd_add_2_to_strtab (abfd, ss, str, str2)
  487.      bfd *abfd;
  488.      struct strtab *ss;
  489.      char *str;
  490.      CONST char *str2;
  491. {
  492.   /* should search first, but for now: */
  493.   /* include the trailing NUL */
  494.   int ln = strlen (str) + strlen (str2) + 1;
  495.  
  496.   /* should this be using obstacks? */
  497.   if (ss->length)
  498.     ss->tab = realloc (ss->tab, ss->length + ln);
  499.   else
  500.     ss->tab = malloc (ln);
  501.  
  502.   BFD_ASSERT (ss->tab != 0);    /* FIXME */
  503.   strcpy (ss->tab + ss->length, str);
  504.   strcpy (ss->tab + ss->length + strlen (str), str2);
  505.   ss->nentries++;
  506.   ss->length += ln;
  507.  
  508.   return ss->length - ln;
  509. }
  510.  
  511. /* ELF .o/exec file reading */
  512.  
  513. /* Create a new bfd section from an ELF section header. */
  514.  
  515. static boolean
  516. bfd_section_from_shdr (abfd, shindex)
  517.      bfd *abfd;
  518.      unsigned int shindex;
  519. {
  520.   Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
  521.   Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
  522.   char *name;
  523.  
  524.   name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
  525.  
  526.   switch (hdr->sh_type)
  527.     {
  528.     case SHT_NULL:
  529.       /* Inactive section. Throw it away.  */
  530.       return true;
  531.  
  532.     case SHT_PROGBITS:    /* Normal section with contents.  */
  533.     case SHT_DYNAMIC:    /* Dynamic linking information.  */
  534.     case SHT_NOBITS:    /* .bss section.  */
  535.     case SHT_HASH:    /* .hash section.  */
  536.       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
  537.  
  538.     case SHT_SYMTAB:        /* A symbol table */
  539.       if (elf_onesymtab (abfd) == shindex)
  540.     return true;
  541.  
  542.       BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
  543.       BFD_ASSERT (elf_onesymtab (abfd) == 0);
  544.       elf_onesymtab (abfd) = shindex;
  545.       elf_tdata (abfd)->symtab_hdr = *hdr;
  546.       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr;
  547.       abfd->flags |= HAS_SYMS;
  548.  
  549.       /* Sometimes a shared object will map in the symbol table.  If
  550.          SHF_ALLOC is set, and this is a shared object, then we also
  551.          treat this section as a BFD section.  We can not base the
  552.          decision purely on SHF_ALLOC, because that flag is sometimes
  553.          set in a relocateable object file, which would confuse the
  554.          linker.  */
  555.       if ((hdr->sh_flags & SHF_ALLOC) != 0
  556.       && (abfd->flags & DYNAMIC) != 0
  557.       && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
  558.     return false;
  559.  
  560.       return true;
  561.  
  562.     case SHT_DYNSYM:        /* A dynamic symbol table */
  563.       if (elf_dynsymtab (abfd) == shindex)
  564.     return true;
  565.  
  566.       BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
  567.       BFD_ASSERT (elf_dynsymtab (abfd) == 0);
  568.       elf_dynsymtab (abfd) = shindex;
  569.       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
  570.       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynsymtab_hdr;
  571.       abfd->flags |= HAS_SYMS;
  572.  
  573.       /* Besides being a symbol table, we also treat this as a regular
  574.      section, so that objcopy can handle it.  */
  575.       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
  576.  
  577.     case SHT_STRTAB:        /* A string table */
  578.       if (hdr->rawdata != NULL)
  579.     return true;
  580.       if (ehdr->e_shstrndx == shindex)
  581.     {
  582.       elf_tdata (abfd)->shstrtab_hdr = *hdr;
  583.       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
  584.       hdr->rawdata = (PTR) & elf_tdata (abfd)->shstrtab_hdr;
  585.       return true;
  586.     }
  587.       {
  588.     unsigned int i;
  589.  
  590.     for (i = 1; i < ehdr->e_shnum; i++)
  591.       {
  592.         Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
  593.         if (hdr2->sh_link == shindex)
  594.           {
  595.         if (! bfd_section_from_shdr (abfd, i))
  596.           return false;
  597.         if (elf_onesymtab (abfd) == i)
  598.           {
  599.             elf_tdata (abfd)->strtab_hdr = *hdr;
  600.             elf_elfsections (abfd)[shindex] =
  601.               &elf_tdata (abfd)->strtab_hdr;
  602.             return true;
  603.           }
  604.         if (elf_dynsymtab (abfd) == i)
  605.           {
  606.             elf_tdata (abfd)->dynstrtab_hdr = *hdr;
  607.             elf_elfsections (abfd)[shindex] =
  608.               &elf_tdata (abfd)->dynstrtab_hdr;
  609.             /* We also treat this as a regular section, so
  610.                that objcopy can handle it.  */
  611.             break;
  612.           }
  613. #if 0 /* Not handling other string tables specially right now.  */
  614.         hdr2 = elf_elfsections (abfd)[i];    /* in case it moved */
  615.         /* We have a strtab for some random other section.  */
  616.         newsect = (asection *) hdr2->rawdata;
  617.         if (!newsect)
  618.           break;
  619.         hdr->rawdata = (PTR) newsect;
  620.         hdr2 = &elf_section_data (newsect)->str_hdr;
  621.         *hdr2 = *hdr;
  622.         elf_elfsections (abfd)[shindex] = hdr2;
  623. #endif
  624.           }
  625.       }
  626.       }
  627.  
  628.       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
  629.  
  630.     case SHT_REL:
  631.     case SHT_RELA:
  632.       /* *These* do a lot of work -- but build no sections!  */
  633.       {
  634.     asection *target_sect;
  635.     Elf_Internal_Shdr *hdr2;
  636.     int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
  637.  
  638.     /* Get the symbol table.  */
  639.     if (! bfd_section_from_shdr (abfd, hdr->sh_link))
  640.       return false;
  641.  
  642.     /* If this reloc section does not use the main symbol table we
  643.        don't treat it as a reloc section.  BFD can't adequately
  644.        represent such a section, so at least for now, we don't
  645.        try.  We just present it as a normal section.  */
  646.     if (hdr->sh_link != elf_onesymtab (abfd))
  647.       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
  648.  
  649.     /* Don't allow REL relocations on a machine that uses RELA and
  650.        vice versa.  */
  651.     /* @@ Actually, the generic ABI does suggest that both might be
  652.        used in one file.  But the four ABI Processor Supplements I
  653.        have access to right now all specify that only one is used on
  654.        each of those architectures.  It's conceivable that, e.g., a
  655.        bunch of absolute 32-bit relocs might be more compact in REL
  656.        form even on a RELA machine...  */
  657.     BFD_ASSERT (use_rela_p
  658.             ? (hdr->sh_type == SHT_RELA
  659.                && hdr->sh_entsize == sizeof (Elf_External_Rela))
  660.             : (hdr->sh_type == SHT_REL
  661.                && hdr->sh_entsize == sizeof (Elf_External_Rel)));
  662.  
  663.     if (! bfd_section_from_shdr (abfd, hdr->sh_info))
  664.       return false;
  665.     target_sect = section_from_elf_index (abfd, hdr->sh_info);
  666.     if (target_sect == NULL
  667.         || elf_section_data (target_sect) == NULL)
  668.       return false;
  669.  
  670.     hdr2 = &elf_section_data (target_sect)->rel_hdr;
  671.     *hdr2 = *hdr;
  672.     elf_elfsections (abfd)[shindex] = hdr2;
  673.     target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
  674.     target_sect->flags |= SEC_RELOC;
  675.     target_sect->relocation = NULL;
  676.     target_sect->rel_filepos = hdr->sh_offset;
  677.     abfd->flags |= HAS_RELOC;
  678.     return true;
  679.       }
  680.       break;
  681.  
  682.     case SHT_NOTE:
  683. #if 0
  684.       fprintf (stderr, "Note Sections not yet supported.\n");
  685.       BFD_FAIL ();
  686. #endif
  687.       break;
  688.  
  689.     case SHT_SHLIB:
  690. #if 0
  691.       fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n");
  692. #endif
  693.       return true;
  694.  
  695.     default:
  696.       /* Check for any processor-specific section types.  */
  697.       {
  698.     struct elf_backend_data *bed = get_elf_backend_data (abfd);
  699.  
  700.     if (bed->elf_backend_section_from_shdr)
  701.       (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
  702.       }
  703.       break;
  704.     }
  705.  
  706.   return true;
  707. }
  708.  
  709. boolean
  710. elf_new_section_hook (abfd, sec)
  711.      bfd *abfd
  712.       ;
  713.      asection *sec;
  714. {
  715.   struct bfd_elf_section_data *sdata;
  716.  
  717.   sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
  718.   if (!sdata)
  719.     {
  720.       bfd_set_error (bfd_error_no_memory);
  721.       return false;
  722.     }
  723.   sec->used_by_bfd = (PTR) sdata;
  724.   memset (sdata, 0, sizeof (*sdata));
  725.   return true;
  726. }
  727.  
  728. /* Create a new bfd section from an ELF program header.
  729.  
  730.    Since program segments have no names, we generate a synthetic name
  731.    of the form segment<NUM>, where NUM is generally the index in the
  732.    program header table.  For segments that are split (see below) we
  733.    generate the names segment<NUM>a and segment<NUM>b.
  734.  
  735.    Note that some program segments may have a file size that is different than
  736.    (less than) the memory size.  All this means is that at execution the
  737.    system must allocate the amount of memory specified by the memory size,
  738.    but only initialize it with the first "file size" bytes read from the
  739.    file.  This would occur for example, with program segments consisting
  740.    of combined data+bss.
  741.  
  742.    To handle the above situation, this routine generates TWO bfd sections
  743.    for the single program segment.  The first has the length specified by
  744.    the file size of the segment, and the second has the length specified
  745.    by the difference between the two sizes.  In effect, the segment is split
  746.    into it's initialized and uninitialized parts.
  747.  
  748.  */
  749.  
  750. static boolean
  751. bfd_section_from_phdr (abfd, hdr, index)
  752.      bfd *abfd;
  753.      Elf_Internal_Phdr *hdr;
  754.      int index;
  755. {
  756.   asection *newsect;
  757.   char *name;
  758.   char namebuf[64];
  759.   int split;
  760.  
  761.   split = ((hdr->p_memsz > 0) &&
  762.        (hdr->p_filesz > 0) &&
  763.        (hdr->p_memsz > hdr->p_filesz));
  764.   sprintf (namebuf, split ? "segment%da" : "segment%d", index);
  765.   name = bfd_alloc (abfd, strlen (namebuf) + 1);
  766.   if (!name)
  767.     {
  768.       bfd_set_error (bfd_error_no_memory);
  769.       return false;
  770.     }
  771.   strcpy (name, namebuf);
  772.   newsect = bfd_make_section (abfd, name);
  773.   if (newsect == NULL)
  774.     return false;
  775.   newsect->vma = hdr->p_vaddr;
  776.   newsect->_raw_size = hdr->p_filesz;
  777.   newsect->filepos = hdr->p_offset;
  778.   newsect->flags |= SEC_HAS_CONTENTS;
  779.   if (hdr->p_type == PT_LOAD)
  780.     {
  781.       newsect->flags |= SEC_ALLOC;
  782.       newsect->flags |= SEC_LOAD;
  783.       if (hdr->p_flags & PF_X)
  784.     {
  785.       /* FIXME: all we known is that it has execute PERMISSION,
  786.          may be data. */
  787.       newsect->flags |= SEC_CODE;
  788.     }
  789.     }
  790.   if (!(hdr->p_flags & PF_W))
  791.     {
  792.       newsect->flags |= SEC_READONLY;
  793.     }
  794.  
  795.   if (split)
  796.     {
  797.       sprintf (namebuf, "segment%db", index);
  798.       name = bfd_alloc (abfd, strlen (namebuf) + 1);
  799.       if (!name)
  800.     {
  801.       bfd_set_error (bfd_error_no_memory);
  802.       return false;
  803.     }
  804.       strcpy (name, namebuf);
  805.       newsect = bfd_make_section (abfd, name);
  806.       if (newsect == NULL)
  807.     return false;
  808.       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
  809.       newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
  810.       if (hdr->p_type == PT_LOAD)
  811.     {
  812.       newsect->flags |= SEC_ALLOC;
  813.       if (hdr->p_flags & PF_X)
  814.         newsect->flags |= SEC_CODE;
  815.     }
  816.       if (!(hdr->p_flags & PF_W))
  817.     newsect->flags |= SEC_READONLY;
  818.     }
  819.  
  820.   return true;
  821. }
  822.  
  823. /* Begin processing a given object.
  824.  
  825.    First we validate the file by reading in the ELF header and checking
  826.    the magic number.  */
  827.  
  828. static INLINE boolean
  829. elf_file_p (x_ehdrp)
  830.      Elf_External_Ehdr *x_ehdrp;
  831. {
  832.   return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
  833.       && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
  834.       && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
  835.       && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
  836. }
  837.  
  838. /* Check to see if the file associated with ABFD matches the target vector
  839.    that ABFD points to.
  840.  
  841.    Note that we may be called several times with the same ABFD, but different
  842.    target vectors, most of which will not match.  We have to avoid leaving
  843.    any side effects in ABFD, or any data it points to (like tdata), if the
  844.    file does not match the target vector.  */
  845.  
  846. const bfd_target *
  847. elf_object_p (abfd)
  848.      bfd *abfd;
  849. {
  850.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  851.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  852.   Elf_External_Shdr x_shdr;    /* Section header table entry, external form */
  853.   Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */
  854.   unsigned int shindex;
  855.   char *shstrtab;        /* Internal copy of section header stringtab */
  856.   struct elf_backend_data *ebd;
  857.   struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
  858.   struct elf_obj_tdata *new_tdata = NULL;
  859.  
  860.   /* Read in the ELF header in external format.  */
  861.  
  862.   if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
  863.     {
  864.       if (bfd_get_error () != bfd_error_system_call)
  865.     goto got_wrong_format_error;
  866.       else
  867.     goto got_no_match;
  868.     }
  869.  
  870.   /* Now check to see if we have a valid ELF file, and one that BFD can
  871.      make use of.  The magic number must match, the address size ('class')
  872.      and byte-swapping must match our XVEC entry, and it must have a
  873.      section header table (FIXME: See comments re sections at top of this
  874.      file). */
  875.  
  876.   if ((elf_file_p (&x_ehdr) == false) ||
  877.       (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
  878.       (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
  879.     goto got_wrong_format_error;
  880.  
  881.   /* Check that file's byte order matches xvec's */
  882.   switch (x_ehdr.e_ident[EI_DATA])
  883.     {
  884.     case ELFDATA2MSB:        /* Big-endian */
  885.       if (!abfd->xvec->header_byteorder_big_p)
  886.     goto got_wrong_format_error;
  887.       break;
  888.     case ELFDATA2LSB:        /* Little-endian */
  889.       if (abfd->xvec->header_byteorder_big_p)
  890.     goto got_wrong_format_error;
  891.       break;
  892.     case ELFDATANONE:        /* No data encoding specified */
  893.     default:            /* Unknown data encoding specified */
  894.       goto got_wrong_format_error;
  895.     }
  896.  
  897.   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
  898.      the tdata pointer in the bfd.  */
  899.  
  900.   new_tdata = ((struct elf_obj_tdata *)
  901.            bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)));
  902.   if (new_tdata == NULL)
  903.     goto got_no_memory_error;
  904.   elf_tdata (abfd) = new_tdata;
  905.  
  906.   /* Now that we know the byte order, swap in the rest of the header */
  907.   i_ehdrp = elf_elfheader (abfd);
  908.   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
  909. #if DEBUG & 1
  910.   elf_debug_file (i_ehdrp);
  911. #endif
  912.  
  913.   /* If there is no section header table, we're hosed. */
  914.   if (i_ehdrp->e_shoff == 0)
  915.     goto got_wrong_format_error;
  916.  
  917.   /* As a simple sanity check, verify that the what BFD thinks is the
  918.      size of each section header table entry actually matches the size
  919.      recorded in the file. */
  920.   if (i_ehdrp->e_shentsize != sizeof (x_shdr))
  921.     goto got_wrong_format_error;
  922.  
  923.   ebd = get_elf_backend_data (abfd);
  924.  
  925.   /* Check that the ELF e_machine field matches what this particular
  926.      BFD format expects.  */
  927.   if (ebd->elf_machine_code != i_ehdrp->e_machine)
  928.     {
  929.       const bfd_target * const *target_ptr;
  930.  
  931.       if (ebd->elf_machine_code != EM_NONE)
  932.     goto got_wrong_format_error;
  933.  
  934.       /* This is the generic ELF target.  Let it match any ELF target
  935.      for which we do not have a specific backend.  */
  936.       for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
  937.     {
  938.       struct elf_backend_data *back;
  939.  
  940.       if ((*target_ptr)->flavour != bfd_target_elf_flavour)
  941.         continue;
  942.       back = (struct elf_backend_data *) (*target_ptr)->backend_data;
  943.       if (back->elf_machine_code == i_ehdrp->e_machine)
  944.         {
  945.           /* target_ptr is an ELF backend which matches this
  946.          object file, so reject the generic ELF target.  */
  947.           goto got_wrong_format_error;
  948.         }
  949.     }
  950.     }
  951.  
  952.   if (i_ehdrp->e_type == ET_EXEC)
  953.     abfd->flags |= EXEC_P;
  954.   else if (i_ehdrp->e_type == ET_DYN)
  955.     abfd->flags |= DYNAMIC;
  956.  
  957.   if (i_ehdrp->e_phnum > 0)
  958.     abfd->flags |= D_PAGED;
  959.  
  960.   if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
  961.     goto got_no_match;
  962.  
  963.   /* Remember the entry point specified in the ELF file header. */
  964.   bfd_get_start_address (abfd) = i_ehdrp->e_entry;
  965.  
  966.   /* Allocate space for a copy of the section header table in
  967.      internal form, seek to the section header table in the file,
  968.      read it in, and convert it to internal form.  */
  969.   i_shdrp = ((Elf_Internal_Shdr *)
  970.          bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum));
  971.   elf_elfsections (abfd) = ((Elf_Internal_Shdr **)
  972.                 bfd_alloc (abfd,
  973.                        sizeof (i_shdrp) * i_ehdrp->e_shnum));
  974.   if (!i_shdrp || !elf_elfsections (abfd))
  975.     goto got_no_memory_error;
  976.   if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0)
  977.     goto got_no_match;
  978.   for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
  979.     {
  980.       if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
  981.     goto got_no_match;
  982.       elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
  983.       elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
  984.     }
  985.   if (i_ehdrp->e_shstrndx)
  986.     {
  987.       if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx))
  988.     goto got_no_match;
  989.     }
  990.  
  991.   /* Read in the string table containing the names of the sections.  We
  992.      will need the base pointer to this table later. */
  993.   /* We read this inline now, so that we don't have to go through
  994.      bfd_section_from_shdr with it (since this particular strtab is
  995.      used to find all of the ELF section names.) */
  996.  
  997.   shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
  998.   if (!shstrtab)
  999.     goto got_no_match;
  1000.  
  1001.   /* Once all of the section headers have been read and converted, we
  1002.      can start processing them.  Note that the first section header is
  1003.      a dummy placeholder entry, so we ignore it.  */
  1004.  
  1005.   for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
  1006.     {
  1007.       if (! bfd_section_from_shdr (abfd, shindex))
  1008.     goto got_no_match;
  1009.     }
  1010.  
  1011.   /* Let the backend double check the format and override global
  1012.      information.  */
  1013.   if (ebd->elf_backend_object_p)
  1014.     {
  1015.       if ((*ebd->elf_backend_object_p) (abfd) == false)
  1016.     goto got_wrong_format_error;
  1017.     }
  1018.  
  1019.   return (abfd->xvec);
  1020.  
  1021. got_wrong_format_error:
  1022.   bfd_set_error (bfd_error_wrong_format);
  1023.   goto got_no_match;
  1024. got_no_memory_error:
  1025.   bfd_set_error (bfd_error_no_memory);
  1026.   goto got_no_match;
  1027. got_no_match:
  1028.   if (new_tdata != NULL
  1029.       && new_tdata->elf_sect_ptr != NULL)
  1030.     bfd_release (abfd, new_tdata->elf_sect_ptr);
  1031.   if (i_shdrp != NULL)
  1032.     bfd_release (abfd, i_shdrp);
  1033.   if (new_tdata != NULL)
  1034.     bfd_release (abfd, new_tdata);
  1035.   elf_tdata (abfd) = preserved_tdata;
  1036.   return (NULL);
  1037. }
  1038.  
  1039.  
  1040. /* ELF .o/exec file writing */
  1041.  
  1042. /* Takes a bfd and a symbol, returns a pointer to the elf specific area
  1043.    of the symbol if there is one.  */
  1044. static INLINE elf_symbol_type *
  1045. elf_symbol_from (ignore_abfd, symbol)
  1046.      bfd *ignore_abfd;
  1047.      asymbol *symbol;
  1048. {
  1049.   if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour)
  1050.     return 0;
  1051.  
  1052.   if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL)
  1053.     return 0;
  1054.  
  1055.   return (elf_symbol_type *) symbol;
  1056. }
  1057.  
  1058. void
  1059. write_relocs (abfd, sec, xxx)
  1060.      bfd *abfd;
  1061.      asection *sec;
  1062.      PTR xxx;
  1063. {
  1064.   Elf_Internal_Shdr *rela_hdr;
  1065.   Elf_External_Rela *outbound_relocas;
  1066.   Elf_External_Rel *outbound_relocs;
  1067.   int idx;
  1068.   int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
  1069.   asymbol *last_sym = 0;
  1070.   int last_sym_idx = 9999999;    /* should always be written before use */
  1071.  
  1072.   if ((sec->flags & SEC_RELOC) == 0)
  1073.     return;
  1074.  
  1075.   /* The linker backend writes the relocs out itself, and sets the
  1076.      reloc_count field to zero to inhibit writing them here.  Also,
  1077.      sometimes the SEC_RELOC flag gets set even when there aren't any
  1078.      relocs.  */
  1079.   if (sec->reloc_count == 0)
  1080.     return;
  1081.  
  1082.   rela_hdr = &elf_section_data (sec)->rel_hdr;
  1083.  
  1084.   rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
  1085.   rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size);
  1086.   if (!rela_hdr->contents)
  1087.     {
  1088.       bfd_set_error (bfd_error_no_memory);
  1089.       abort ();            /* FIXME */
  1090.     }
  1091.  
  1092.   /* orelocation has the data, reloc_count has the count... */
  1093.   if (use_rela_p)
  1094.     {
  1095.       outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
  1096.  
  1097.       for (idx = 0; idx < sec->reloc_count; idx++)
  1098.     {
  1099.       Elf_Internal_Rela dst_rela;
  1100.       Elf_External_Rela *src_rela;
  1101.       arelent *ptr;
  1102.       asymbol *sym;
  1103.       int n;
  1104.  
  1105.       ptr = sec->orelocation[idx];
  1106.       src_rela = outbound_relocas + idx;
  1107.       if (!(abfd->flags & EXEC_P))
  1108.         dst_rela.r_offset = ptr->address - sec->vma;
  1109.       else
  1110.         dst_rela.r_offset = ptr->address;
  1111.  
  1112.       sym = *ptr->sym_ptr_ptr;
  1113.       if (sym == last_sym)
  1114.         n = last_sym_idx;
  1115.       else
  1116.         {
  1117.           last_sym = sym;
  1118.           last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
  1119.         }
  1120.       dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
  1121.  
  1122.       dst_rela.r_addend = ptr->addend;
  1123.       elf_swap_reloca_out (abfd, &dst_rela, src_rela);
  1124.     }
  1125.     }
  1126.   else
  1127.     /* REL relocations */
  1128.     {
  1129.       outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
  1130.  
  1131.       for (idx = 0; idx < sec->reloc_count; idx++)
  1132.     {
  1133.       Elf_Internal_Rel dst_rel;
  1134.       Elf_External_Rel *src_rel;
  1135.       arelent *ptr;
  1136.       int n;
  1137.       asymbol *sym;
  1138.  
  1139.       ptr = sec->orelocation[idx];
  1140.       sym = *ptr->sym_ptr_ptr;
  1141.       src_rel = outbound_relocs + idx;
  1142.       if (!(abfd->flags & EXEC_P))
  1143.         dst_rel.r_offset = ptr->address - sec->vma;
  1144.       else
  1145.         dst_rel.r_offset = ptr->address;
  1146.  
  1147.       if (sym == last_sym)
  1148.         n = last_sym_idx;
  1149.       else
  1150.         {
  1151.           last_sym = sym;
  1152.           last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
  1153.         }
  1154.       dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
  1155.  
  1156.       elf_swap_reloc_out (abfd, &dst_rel, src_rel);
  1157.     }
  1158.     }
  1159. }
  1160.  
  1161. /* Set up an ELF internal section header for a section.  */
  1162.  
  1163. /*ARGSUSED*/
  1164. static void
  1165. elf_fake_sections (abfd, asect, ignore)
  1166.      bfd *abfd;
  1167.      asection *asect;
  1168.      PTR ignore;
  1169. {
  1170.   Elf_Internal_Shdr *this_hdr;
  1171.  
  1172.   this_hdr = &elf_section_data (asect)->this_hdr;
  1173.  
  1174.   this_hdr->sh_name = bfd_add_to_strtab (abfd, elf_shstrtab (abfd),
  1175.                      asect->name);
  1176.   if (this_hdr->sh_name == (unsigned long) -1)
  1177.     abort (); /* FIXME */
  1178.  
  1179.   this_hdr->sh_flags = 0;
  1180.   if ((asect->flags & SEC_ALLOC) != 0)
  1181.     this_hdr->sh_addr = asect->vma;
  1182.   else
  1183.     this_hdr->sh_addr = 0;
  1184.   this_hdr->sh_offset = 0;
  1185.   this_hdr->sh_size = asect->_raw_size;
  1186.   this_hdr->sh_link = 0;
  1187.   this_hdr->sh_info = 0;
  1188.   this_hdr->sh_addralign = 1 << asect->alignment_power;
  1189.   this_hdr->sh_entsize = 0;
  1190.  
  1191.   this_hdr->rawdata = (PTR) asect;
  1192.   this_hdr->contents = NULL;
  1193.   this_hdr->size = 0;
  1194.  
  1195.   /* FIXME: This should not be based on section names.  */
  1196.   if (strcmp (asect->name, ".dynstr") == 0)
  1197.     this_hdr->sh_type = SHT_STRTAB;
  1198.   else if (strcmp (asect->name, ".hash") == 0)
  1199.     {
  1200.       this_hdr->sh_type = SHT_HASH;
  1201.       this_hdr->sh_entsize = ARCH_SIZE / 8;
  1202.     }
  1203.   else if (strcmp (asect->name, ".dynsym") == 0)
  1204.     {
  1205.       this_hdr->sh_type = SHT_DYNSYM;
  1206.       this_hdr->sh_entsize = sizeof (Elf_External_Sym);
  1207.     }
  1208.   else if (strcmp (asect->name, ".dynamic") == 0)
  1209.     {
  1210.       this_hdr->sh_type = SHT_DYNAMIC;
  1211.       this_hdr->sh_entsize = sizeof (Elf_External_Dyn);
  1212.     }
  1213.   else if (strncmp (asect->name, ".rela", 5) == 0
  1214.        && get_elf_backend_data (abfd)->use_rela_p)
  1215.     {
  1216.       this_hdr->sh_type = SHT_RELA;
  1217.       this_hdr->sh_entsize = sizeof (Elf_External_Rela);
  1218.     }
  1219.   else if (strncmp (asect->name, ".rel", 4) == 0
  1220.        && ! get_elf_backend_data (abfd)->use_rela_p)
  1221.     {
  1222.       this_hdr->sh_type = SHT_REL;
  1223.       this_hdr->sh_entsize = sizeof (Elf_External_Rel);
  1224.     }
  1225.   else if (strcmp (asect->name, ".note") == 0)
  1226.     this_hdr->sh_type = SHT_NOTE;
  1227.   else if (strncmp (asect->name, ".stab", 5) == 0
  1228.        && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
  1229.     this_hdr->sh_type = SHT_STRTAB;
  1230.   else if ((asect->flags & SEC_ALLOC) != 0
  1231.        && (asect->flags & SEC_LOAD) != 0)
  1232.     this_hdr->sh_type = SHT_PROGBITS;
  1233.   else if ((asect->flags & SEC_ALLOC) != 0
  1234.        && ((asect->flags & SEC_LOAD) == 0))
  1235.     {
  1236.       BFD_ASSERT (strcmp (asect->name, ".bss") == 0
  1237.           || strcmp (asect->name, ".sbss") == 0);
  1238.       this_hdr->sh_type = SHT_NOBITS;
  1239.     }
  1240.   else
  1241.     {
  1242.       /* Who knows?  */
  1243.       this_hdr->sh_type = SHT_PROGBITS;
  1244.     }
  1245.  
  1246.   if ((asect->flags & SEC_ALLOC) != 0)
  1247.     this_hdr->sh_flags |= SHF_ALLOC;
  1248.   if ((asect->flags & SEC_READONLY) == 0)
  1249.     this_hdr->sh_flags |= SHF_WRITE;
  1250.   if ((asect->flags & SEC_CODE) != 0)
  1251.     this_hdr->sh_flags |= SHF_EXECINSTR;
  1252.  
  1253.   /* Check for processor-specific section types.  */
  1254.   {
  1255.     struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1256.  
  1257.     if (bed->elf_backend_fake_sections)
  1258.       (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
  1259.   }
  1260.  
  1261.   /* If the section has relocs, set up a section header for the
  1262.      SHT_REL[A] section.  */
  1263.   if ((asect->flags & SEC_RELOC) != 0)
  1264.     {
  1265.       Elf_Internal_Shdr *rela_hdr;
  1266.       int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
  1267.  
  1268.       rela_hdr = &elf_section_data (asect)->rel_hdr;
  1269.       rela_hdr->sh_name =
  1270.     bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
  1271.                  use_rela_p ? ".rela" : ".rel",
  1272.                  asect->name);
  1273.       rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
  1274.       rela_hdr->sh_entsize = (use_rela_p
  1275.                   ? sizeof (Elf_External_Rela)
  1276.                   : sizeof (Elf_External_Rel));
  1277.       rela_hdr->sh_addralign = FILE_ALIGN;
  1278.       rela_hdr->sh_flags = 0;
  1279.       rela_hdr->sh_addr = 0;
  1280.       rela_hdr->sh_size = 0;
  1281.       rela_hdr->sh_offset = 0;
  1282.       rela_hdr->size = 0;
  1283.     }
  1284. }
  1285.  
  1286. /* Assign all ELF section numbers.  The dummy first section is handled here
  1287.    too.  The link/info pointers for the standard section types are filled
  1288.    in here too, while we're at it.  */
  1289.  
  1290. static boolean
  1291. assign_section_numbers (abfd)
  1292.      bfd *abfd;
  1293. {
  1294.   struct elf_obj_tdata *t = elf_tdata (abfd);
  1295.   asection *sec;
  1296.   unsigned int section_number;
  1297.   Elf_Internal_Shdr **i_shdrp;
  1298.  
  1299.   section_number = 1;
  1300.  
  1301.   for (sec = abfd->sections; sec; sec = sec->next)
  1302.     {
  1303.       struct bfd_elf_section_data *d = elf_section_data (sec);
  1304.  
  1305.       d->this_idx = section_number++;
  1306.       if ((sec->flags & SEC_RELOC) == 0)
  1307.     d->rel_idx = 0;
  1308.       else
  1309.     d->rel_idx = section_number++;
  1310.     }
  1311.  
  1312.   t->shstrtab_section = section_number++;
  1313.   elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
  1314.   t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length;
  1315.   t->shstrtab_hdr.contents = (PTR) elf_shstrtab (abfd)->tab;
  1316.  
  1317.   if (abfd->symcount > 0)
  1318.     {
  1319.       t->symtab_section = section_number++;
  1320.       t->strtab_section = section_number++;
  1321.     }
  1322.  
  1323.   elf_elfheader (abfd)->e_shnum = section_number;
  1324.  
  1325.   /* Set up the list of section header pointers, in agreement with the
  1326.      indices.  */
  1327.   i_shdrp = ((Elf_Internal_Shdr **)
  1328.          bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
  1329.   if (i_shdrp == NULL)
  1330.     {
  1331.       bfd_set_error (bfd_error_no_memory);
  1332.       return false;
  1333.     }
  1334.  
  1335.   i_shdrp[0] = ((Elf_Internal_Shdr *)
  1336.         bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
  1337.   if (i_shdrp[0] == NULL)
  1338.     {
  1339.       bfd_release (abfd, i_shdrp);
  1340.       bfd_set_error (bfd_error_no_memory);
  1341.       return false;
  1342.     }
  1343.   memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
  1344.  
  1345.   elf_elfsections (abfd) = i_shdrp;
  1346.  
  1347.   i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
  1348.   if (abfd->symcount > 0)
  1349.     {
  1350.       i_shdrp[t->symtab_section] = &t->symtab_hdr;
  1351.       i_shdrp[t->strtab_section] = &t->strtab_hdr;
  1352.       t->symtab_hdr.sh_link = t->strtab_section;
  1353.     }
  1354.   for (sec = abfd->sections; sec; sec = sec->next)
  1355.     {
  1356.       struct bfd_elf_section_data *d = elf_section_data (sec);
  1357.       asection *s;
  1358.       const char *name;
  1359.  
  1360.       i_shdrp[d->this_idx] = &d->this_hdr;
  1361.       if (d->rel_idx != 0)
  1362.     i_shdrp[d->rel_idx] = &d->rel_hdr;
  1363.  
  1364.       /* Fill in the sh_link and sh_info fields while we're at it.  */
  1365.  
  1366.       /* sh_link of a reloc section is the section index of the symbol
  1367.      table.  sh_info is the section index of the section to which
  1368.      the relocation entries apply.  */
  1369.       if (d->rel_idx != 0)
  1370.     {
  1371.       d->rel_hdr.sh_link = t->symtab_section;
  1372.       d->rel_hdr.sh_info = d->this_idx;
  1373.     }
  1374.  
  1375.       switch (d->this_hdr.sh_type)
  1376.     {
  1377.     case SHT_REL:
  1378.     case SHT_RELA:
  1379.       /* A reloc section which we are treating as a normal BFD
  1380.          section.  sh_link is the section index of the symbol
  1381.          table.  sh_info is the section index of the section to
  1382.          which the relocation entries apply.  We assume that an
  1383.          allocated reloc section uses the dynamic symbol table.
  1384.          FIXME: How can we be sure?  */
  1385.       s = bfd_get_section_by_name (abfd, ".dynsym");
  1386.       if (s != NULL)
  1387.         d->this_hdr.sh_link = elf_section_data (s)->this_idx;
  1388.  
  1389.       /* We look up the section the relocs apply to by name.  */
  1390.       name = sec->name;
  1391.       if (d->this_hdr.sh_type == SHT_REL)
  1392.         name += 4;
  1393.       else
  1394.         name += 5;
  1395.       s = bfd_get_section_by_name (abfd, name);
  1396.       if (s != NULL)
  1397.         d->this_hdr.sh_info = elf_section_data (s)->this_idx;
  1398.       break;
  1399.  
  1400.     case SHT_STRTAB:
  1401.       /* We assume that a section named .stab*str is a stabs
  1402.          string section.  We look for a section with the same name
  1403.          but without the trailing ``str'', and set its sh_link
  1404.          field to point to this section.  */
  1405.       if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
  1406.           && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
  1407.         {
  1408.           size_t len;
  1409.           char *alc;
  1410.  
  1411.           len = strlen (sec->name);
  1412.           alc = (char *) malloc (len - 2);
  1413.           if (alc == NULL)
  1414.         {
  1415.           bfd_set_error (bfd_error_no_memory);
  1416.           return false;
  1417.         }
  1418.           strncpy (alc, sec->name, len - 3);
  1419.           alc[len - 3] = '\0';
  1420.           s = bfd_get_section_by_name (abfd, alc);
  1421.           free (alc);
  1422.           if (s != NULL)
  1423.         {
  1424.           elf_section_data (s)->this_hdr.sh_link = d->this_idx;
  1425.  
  1426.           /* This is a .stab section.  */
  1427.           elf_section_data (s)->this_hdr.sh_entsize =
  1428.             4 + 2 * (ARCH_SIZE / 8);
  1429.         }
  1430.         }
  1431.       break;
  1432.  
  1433.     case SHT_DYNAMIC:
  1434.     case SHT_DYNSYM:
  1435.       /* sh_link is the section header index of the string table
  1436.          used for the dynamic entries or symbol table.  */
  1437.       s = bfd_get_section_by_name (abfd, ".dynstr");
  1438.       if (s != NULL)
  1439.         d->this_hdr.sh_link = elf_section_data (s)->this_idx;
  1440.       break;
  1441.  
  1442.     case SHT_HASH:
  1443.       /* sh_link is the section header index of the symbol table
  1444.          this hash table is for.  */
  1445.       s = bfd_get_section_by_name (abfd, ".dynsym");
  1446.       if (s != NULL)
  1447.         d->this_hdr.sh_link = elf_section_data (s)->this_idx;
  1448.       break;
  1449.     }
  1450.     }
  1451.  
  1452.   return true;
  1453. }
  1454.  
  1455. /* Map symbol from it's internal number to the external number, moving
  1456.    all local symbols to be at the head of the list.  */
  1457.  
  1458. static INLINE int
  1459. sym_is_global (abfd, sym)
  1460.      bfd *abfd;
  1461.      asymbol *sym;
  1462. {
  1463.   /* If the backend has a special mapping, use it.  */
  1464.   if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
  1465.     return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
  1466.         (abfd, sym));
  1467.  
  1468.   if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
  1469.     {
  1470.       if (sym->flags & BSF_LOCAL)
  1471.     abort ();
  1472.       return 1;
  1473.     }
  1474.   if (sym->section == 0)
  1475.     {
  1476.       /* Is this valid?  */
  1477.       abort ();
  1478.  
  1479.       return 1;
  1480.     }
  1481.   if (bfd_is_und_section (sym->section))
  1482.     return 1;
  1483.   if (bfd_is_com_section (sym->section))
  1484.     return 1;
  1485.   if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE))
  1486.     return 0;
  1487.   return 0;
  1488. }
  1489.  
  1490. static boolean
  1491. elf_map_symbols (abfd)
  1492.      bfd *abfd;
  1493. {
  1494.   int symcount = bfd_get_symcount (abfd);
  1495.   asymbol **syms = bfd_get_outsymbols (abfd);
  1496.   asymbol **sect_syms;
  1497.   int num_locals = 0;
  1498.   int num_globals = 0;
  1499.   int num_locals2 = 0;
  1500.   int num_globals2 = 0;
  1501.   int max_index = 0;
  1502.   int num_sections = 0;
  1503.   Elf_Sym_Extra *sym_extra;
  1504.   int idx;
  1505.   asection *asect;
  1506.  
  1507. #ifdef DEBUG
  1508.   fprintf (stderr, "elf_map_symbols\n");
  1509.   fflush (stderr);
  1510. #endif
  1511.  
  1512.   /* Add local symbols for each section for which there are relocs.
  1513.      FIXME: How can we tell which sections have relocs at this point?
  1514.      Will reloc_count always be accurate?  Actually, I think most ELF
  1515.      targets create section symbols for all sections anyhow.  */
  1516.   for (asect = abfd->sections; asect; asect = asect->next)
  1517.     {
  1518.       if (max_index < asect->index)
  1519.     max_index = asect->index;
  1520.     }
  1521.  
  1522.   max_index++;
  1523.   elf_num_section_syms (abfd) = max_index;
  1524.   sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
  1525.   elf_section_syms (abfd) = sect_syms;
  1526.  
  1527.   if (sect_syms == 0)
  1528.     {
  1529.       bfd_set_error (bfd_error_no_memory);
  1530.       return false;
  1531.     }
  1532.  
  1533.   for (asect = abfd->sections; asect; asect = asect->next)
  1534.     {
  1535.       asymbol *sym = bfd_make_empty_symbol (abfd);
  1536.       if (!sym)
  1537.     {
  1538.       bfd_set_error (bfd_error_no_memory);
  1539.       return false;
  1540.     }
  1541.       sym->the_bfd = abfd;
  1542.       sym->name = asect->name;
  1543.       sym->value = asect->vma;
  1544.       sym->flags = BSF_SECTION_SYM;
  1545.       sym->section = asect;
  1546.       sect_syms[asect->index] = sym;
  1547.       num_sections++;
  1548. #ifdef DEBUG
  1549.       fprintf (stderr,
  1550.            "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
  1551.            asect->name, (long) asect->vma, asect->index, (long) asect);
  1552. #endif
  1553.     }
  1554.  
  1555.   if (num_sections)
  1556.     {
  1557.       if (syms)
  1558.     {
  1559.       asymbol **osyms = syms;
  1560.       syms = (asymbol **) bfd_alloc (abfd,
  1561.                      ((symcount + num_sections + 1)
  1562.                       * sizeof (asymbol *)));
  1563.       memcpy (syms, osyms, symcount * sizeof (asymbol *));
  1564.     }
  1565.       else
  1566.     syms = (asymbol **) bfd_alloc (abfd,
  1567.                    (num_sections + 1) * sizeof (asymbol *));
  1568.       if (!syms)
  1569.     {
  1570.       bfd_set_error (bfd_error_no_memory);
  1571.       return false;
  1572.     }
  1573.  
  1574.       for (asect = abfd->sections; asect; asect = asect->next)
  1575.     {
  1576.       if (sect_syms[asect->index])
  1577.         syms[symcount++] = sect_syms[asect->index];
  1578.     }
  1579.  
  1580.       syms[symcount] = (asymbol *) 0;
  1581.       bfd_set_symtab (abfd, syms, symcount);
  1582.     }
  1583.  
  1584.   elf_sym_extra (abfd) = sym_extra
  1585.     = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra));
  1586.   if (!sym_extra)
  1587.     {
  1588.       bfd_set_error (bfd_error_no_memory);
  1589.       return false;
  1590.     }
  1591.  
  1592.   /* Identify and classify all of the symbols.  */
  1593.   for (idx = 0; idx < symcount; idx++)
  1594.     {
  1595.       if (!sym_is_global (abfd, syms[idx]))
  1596.     num_locals++;
  1597.       else
  1598.     num_globals++;
  1599.     }
  1600.  
  1601.   /* Now provide mapping information.  Add +1 for skipping over the
  1602.      dummy symbol.  */
  1603.   for (idx = 0; idx < symcount; idx++)
  1604.     {
  1605.       syms[idx]->udata = (PTR) & sym_extra[idx];
  1606.       if (!sym_is_global (abfd, syms[idx]))
  1607.     sym_extra[idx].elf_sym_num = 1 + num_locals2++;
  1608.       else
  1609.     sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++;
  1610.     }
  1611.  
  1612.   elf_num_locals (abfd) = num_locals;
  1613.   elf_num_globals (abfd) = num_globals;
  1614.   return true;
  1615. }
  1616.  
  1617. /* Compute the file positions we are going to put the sections at, and
  1618.    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
  1619.    is not NULL, this is being called by the ELF backend linker.  */
  1620.  
  1621. static boolean
  1622. elf_compute_section_file_positions (abfd, link_info)
  1623.      bfd *abfd;
  1624.      struct bfd_link_info *link_info;
  1625. {
  1626.   struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1627.   Elf_Internal_Shdr *shstrtab_hdr;
  1628.  
  1629.   if (abfd->output_has_begun)
  1630.     return true;
  1631.  
  1632.   /* Do any elf backend specific processing first.  */
  1633.   if (bed->elf_backend_begin_write_processing)
  1634.     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
  1635.  
  1636.   if (! prep_headers (abfd))
  1637.     return false;
  1638.  
  1639.   bfd_map_over_sections (abfd, elf_fake_sections, 0);
  1640.  
  1641.   if (!assign_section_numbers (abfd))
  1642.     return false;
  1643.  
  1644.   /* The backend linker builds symbol table information itself.  */
  1645.   if (link_info == NULL)
  1646.     {
  1647.       if (! swap_out_syms (abfd))
  1648.     return false;
  1649.     }
  1650.  
  1651.   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
  1652.   /* sh_name was set in prep_headers.  */
  1653.   shstrtab_hdr->sh_type = SHT_STRTAB;
  1654.   shstrtab_hdr->sh_flags = 0;
  1655.   shstrtab_hdr->sh_addr = 0;
  1656.   shstrtab_hdr->sh_size = elf_shstrtab (abfd)->length;
  1657.   shstrtab_hdr->sh_entsize = 0;
  1658.   shstrtab_hdr->sh_link = 0;
  1659.   shstrtab_hdr->sh_info = 0;
  1660.   /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs.  */
  1661.   shstrtab_hdr->sh_addralign = 1;
  1662.   shstrtab_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
  1663.  
  1664.   if (!assign_file_positions_except_relocs (abfd,
  1665.                         link_info == NULL ? true : false))
  1666.     return false;
  1667.  
  1668.   abfd->output_has_begun = true;
  1669.  
  1670.   return true;
  1671. }
  1672.  
  1673.  
  1674. /* Align to the maximum file alignment that could be required for any
  1675.    ELF data structure.  */
  1676.  
  1677. static INLINE file_ptr
  1678. align_file_position (off)
  1679.      file_ptr off;
  1680. {
  1681.   return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1);
  1682. }
  1683.  
  1684. /* Assign a file position to a section, optionally aligning to the
  1685.    required section alignment.  */
  1686.  
  1687. static INLINE file_ptr
  1688. assign_file_position_for_section (i_shdrp, offset, align)
  1689.      Elf_Internal_Shdr *i_shdrp;
  1690.      file_ptr offset;
  1691.      boolean align;
  1692. {
  1693.   if (align)
  1694.     {
  1695.       unsigned int al;
  1696.  
  1697.       al = i_shdrp->sh_addralign;
  1698.       if (al > 1)
  1699.     offset = BFD_ALIGN (offset, al);
  1700.     }
  1701.   i_shdrp->sh_offset = offset;
  1702.   if (i_shdrp->rawdata != NULL)
  1703.     ((asection *) i_shdrp->rawdata)->filepos = offset;
  1704.   if (i_shdrp->sh_type != SHT_NOBITS)
  1705.     offset += i_shdrp->sh_size;
  1706.   return offset;
  1707. }
  1708.  
  1709. /* Get the size of the program header.  This is called by the linker
  1710.    before any of the section VMA's are set, so it can't calculate the
  1711.    correct value for a strange memory layout.  */
  1712.  
  1713. static bfd_size_type
  1714. get_program_header_size (abfd)
  1715.      bfd *abfd;
  1716. {
  1717.   size_t segs;
  1718.   asection *s;
  1719.  
  1720.   /* Assume we will need exactly two PT_LOAD segments: one for text
  1721.      and one for data.  */
  1722.   segs = 2;
  1723.  
  1724.   s = bfd_get_section_by_name (abfd, ".interp");
  1725.   if (s != NULL && (s->flags & SEC_LOAD) != 0)
  1726.     {
  1727.       /* If we have a loadable interpreter section, we need a
  1728.      PT_INTERP segment.  In this case, assume we also need a
  1729.      PT_PHDR segment, although that may not be true for all
  1730.      targets.  */
  1731.       segs += 2;
  1732.     }
  1733.  
  1734.   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
  1735.     {
  1736.       /* We need a PT_DYNAMIC segment.  */
  1737.       ++segs;
  1738.     }
  1739.  
  1740.   return segs * sizeof (Elf_External_Phdr);
  1741. }
  1742.  
  1743. /* Create the program header.  OFF is the file offset where the
  1744.    program header should be written.  FIRST is the first loadable ELF
  1745.    section.  PHDR_SIZE is the size of the program header as returned
  1746.    by get_program_header_size.  */
  1747.  
  1748. static file_ptr
  1749. map_program_segments (abfd, off, first, phdr_size)
  1750.      bfd *abfd;
  1751.      file_ptr off;
  1752.      Elf_Internal_Shdr *first;
  1753.      bfd_size_type phdr_size;
  1754. {
  1755.   Elf_Internal_Phdr phdrs[10];
  1756.   unsigned int phdr_count;
  1757.   Elf_Internal_Phdr *phdr;
  1758.   int phdr_size_adjust;
  1759.   unsigned int i;
  1760.   Elf_Internal_Shdr **hdrpp;
  1761.   asection *sinterp, *sdyn;
  1762.   unsigned int last_type;
  1763.   Elf_Internal_Ehdr *i_ehdrp;
  1764.  
  1765.   BFD_ASSERT ((abfd->flags & (EXEC_P | DYNAMIC)) != 0);
  1766.   BFD_ASSERT (phdr_size / sizeof (Elf_Internal_Phdr)
  1767.           <= sizeof phdrs / sizeof (phdrs[0]));
  1768.  
  1769.   phdr_count = 0;
  1770.   phdr = phdrs;
  1771.  
  1772.   phdr_size_adjust = 0;
  1773.  
  1774.   /* If we have a loadable .interp section, we must create a PT_INTERP
  1775.      segment which must precede all PT_LOAD segments.  We assume that
  1776.      we must also create a PT_PHDR segment, although that may not be
  1777.      true for all targets.  */
  1778.   sinterp = bfd_get_section_by_name (abfd, ".interp");
  1779.   if (sinterp != NULL && (sinterp->flags & SEC_LOAD) != 0)
  1780.     {
  1781.       BFD_ASSERT (first != NULL);
  1782.  
  1783.       phdr->p_type = PT_PHDR;
  1784.  
  1785.       phdr->p_offset = off;
  1786.  
  1787.       /* Account for any adjustment made because of the alignment of
  1788.      the first loadable section.  */
  1789.       phdr_size_adjust = (first->sh_offset - phdr_size) - off;
  1790.       BFD_ASSERT (phdr_size_adjust >= 0 && phdr_size_adjust < 128);
  1791.  
  1792.       /* The program header precedes all loadable sections.  This lets
  1793.      us compute its loadable address.  This depends on the linker
  1794.      script.  */
  1795.       phdr->p_vaddr = first->sh_addr - (phdr_size + phdr_size_adjust);
  1796.  
  1797.       phdr->p_paddr = 0;
  1798.       phdr->p_filesz = phdr_size;
  1799.       phdr->p_memsz = phdr_size;
  1800.  
  1801.       /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
  1802.       phdr->p_flags = PF_R | PF_X;
  1803.  
  1804.       phdr->p_align = FILE_ALIGN;
  1805.       BFD_ASSERT ((phdr->p_vaddr - phdr->p_offset) % FILE_ALIGN == 0);
  1806.  
  1807.       /* Include the ELF header in the first loadable segment.  */
  1808.       phdr_size_adjust += off;
  1809.  
  1810.       ++phdr_count;
  1811.       ++phdr;
  1812.  
  1813.       phdr->p_type = PT_INTERP;
  1814.       phdr->p_offset = sinterp->filepos;
  1815.       phdr->p_vaddr = sinterp->vma;
  1816.       phdr->p_paddr = 0;
  1817.       phdr->p_filesz = sinterp->_raw_size;
  1818.       phdr->p_memsz = sinterp->_raw_size;
  1819.       phdr->p_flags = PF_R;
  1820.       phdr->p_align = 1 << bfd_get_section_alignment (abfd, sinterp);
  1821.  
  1822.       ++phdr_count;
  1823.       ++phdr;
  1824.     }
  1825.  
  1826.   /* Look through the sections to see how they will be divided into
  1827.      program segments.  The sections must be arranged in order by
  1828.      sh_addr for this to work correctly.  */
  1829.   phdr->p_type = PT_NULL;
  1830.   last_type = SHT_PROGBITS;
  1831.   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
  1832.        i < elf_elfheader (abfd)->e_shnum;
  1833.        i++, hdrpp++)
  1834.     {
  1835.       Elf_Internal_Shdr *hdr;
  1836.  
  1837.       hdr = *hdrpp;
  1838.  
  1839.       /* Ignore any section which will not be part of the process
  1840.      image.  */
  1841.       if ((hdr->sh_flags & SHF_ALLOC) == 0)
  1842.     continue;
  1843.  
  1844.       /* If this section fits in the segment we are constructing, add
  1845.      it in.  */
  1846.       if (phdr->p_type != PT_NULL
  1847.       && (hdr->sh_offset - (phdr->p_offset + phdr->p_memsz)
  1848.           == hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz))
  1849.       && (last_type != SHT_NOBITS || hdr->sh_type == SHT_NOBITS))
  1850.     {
  1851.       bfd_size_type adjust;
  1852.  
  1853.       adjust = hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz);
  1854.       phdr->p_memsz += hdr->sh_size + adjust;
  1855.       if (hdr->sh_type != SHT_NOBITS)
  1856.         phdr->p_filesz += hdr->sh_size + adjust;
  1857.       if ((hdr->sh_flags & SHF_WRITE) != 0)
  1858.         phdr->p_flags |= PF_W;
  1859.       if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
  1860.         phdr->p_flags |= PF_X;
  1861.       last_type = hdr->sh_type;
  1862.       continue;
  1863.     }
  1864.  
  1865.       /* If we have a segment, move to the next one.  */
  1866.       if (phdr->p_type != PT_NULL)
  1867.     {
  1868.       ++phdr;
  1869.       ++phdr_count;
  1870.     }
  1871.  
  1872.       /* Start a new segment.  */
  1873.       phdr->p_type = PT_LOAD;
  1874.       phdr->p_offset = hdr->sh_offset;
  1875.       phdr->p_vaddr = hdr->sh_addr;
  1876.       phdr->p_paddr = 0;
  1877.       if (hdr->sh_type == SHT_NOBITS)
  1878.     phdr->p_filesz = 0;
  1879.       else
  1880.     phdr->p_filesz = hdr->sh_size;
  1881.       phdr->p_memsz = hdr->sh_size;
  1882.       phdr->p_flags = PF_R;
  1883.       if ((hdr->sh_flags & SHF_WRITE) != 0)
  1884.     phdr->p_flags |= PF_W;
  1885.       if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
  1886.     phdr->p_flags |= PF_X;
  1887.       phdr->p_align = get_elf_backend_data (abfd)->maxpagesize;
  1888.  
  1889.       if (hdr == first
  1890.       && sinterp != NULL
  1891.       && (sinterp->flags & SEC_LOAD) != 0)
  1892.     {
  1893.       phdr->p_offset -= phdr_size + phdr_size_adjust;
  1894.       phdr->p_vaddr -= phdr_size + phdr_size_adjust;
  1895.       phdr->p_filesz += phdr_size + phdr_size_adjust;
  1896.       phdr->p_memsz += phdr_size + phdr_size_adjust;
  1897.     }
  1898.  
  1899.       last_type = hdr->sh_type;
  1900.     }
  1901.  
  1902.   if (phdr->p_type != PT_NULL)
  1903.     {
  1904.       ++phdr;
  1905.       ++phdr_count;
  1906.     }
  1907.  
  1908.   /* If we have a .dynamic section, create a PT_DYNAMIC segment.  */
  1909.   sdyn = bfd_get_section_by_name (abfd, ".dynamic");
  1910.   if (sdyn != NULL && (sdyn->flags & SEC_LOAD) != 0)
  1911.     {
  1912.       phdr->p_type = PT_DYNAMIC;
  1913.       phdr->p_offset = sdyn->filepos;
  1914.       phdr->p_vaddr = sdyn->vma;
  1915.       phdr->p_paddr = 0;
  1916.       phdr->p_filesz = sdyn->_raw_size;
  1917.       phdr->p_memsz = sdyn->_raw_size;
  1918.       phdr->p_flags = PF_R;
  1919.       if ((sdyn->flags & SEC_READONLY) == 0)
  1920.     phdr->p_flags |= PF_W;
  1921.       if ((sdyn->flags & SEC_CODE) != 0)
  1922.     phdr->p_flags |= PF_X;
  1923.       phdr->p_align = 1 << bfd_get_section_alignment (abfd, sdyn);
  1924.  
  1925.       ++phdr;
  1926.       ++phdr_count;
  1927.     }
  1928.  
  1929.   /* Make sure the return value from get_program_header_size matches
  1930.      what we computed here.  Actually, it's OK if we allocated too
  1931.      much space in the program header.  */
  1932.   if (phdr_count > phdr_size / sizeof (Elf_External_Phdr))
  1933.     abort ();
  1934.  
  1935.   /* Set up program header information.  */
  1936.   i_ehdrp = elf_elfheader (abfd);
  1937.   i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
  1938.   i_ehdrp->e_phoff = off;
  1939.   i_ehdrp->e_phnum = phdr_count;
  1940.  
  1941.   /* Save the program headers away.  I don't think anybody uses this
  1942.      information right now.  */
  1943.   elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *)
  1944.                 bfd_alloc (abfd,
  1945.                        (phdr_count
  1946.                     * sizeof (Elf_Internal_Phdr))));
  1947.   if (elf_tdata (abfd)->phdr == NULL && phdr_count != 0)
  1948.     {
  1949.       bfd_set_error (bfd_error_no_memory);
  1950.       return (file_ptr) -1;
  1951.     }
  1952.   memcpy (elf_tdata (abfd)->phdr, phdrs,
  1953.       phdr_count * sizeof (Elf_Internal_Phdr));
  1954.  
  1955.   /* Write out the program headers.  */
  1956.   if (bfd_seek (abfd, off, SEEK_SET) != 0)
  1957.     return (file_ptr) -1;
  1958.  
  1959.   for (i = 0, phdr = phdrs; i < phdr_count; i++, phdr++)
  1960.     {
  1961.       Elf_External_Phdr extphdr;
  1962.  
  1963.       elf_swap_phdr_out (abfd, phdr, &extphdr);
  1964.       if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), 1, abfd)
  1965.       != sizeof (Elf_External_Phdr))
  1966.     return (file_ptr) -1;
  1967.     }
  1968.  
  1969.   return off + phdr_count * sizeof (Elf_External_Phdr);
  1970. }
  1971.  
  1972. /* Work out the file positions of all the sections.  This is called by
  1973.    elf_compute_section_file_positions.  All the section sizes and VMAs
  1974.    must be known before this is called.
  1975.  
  1976.    We do not consider reloc sections at this point, unless they form
  1977.    part of the loadable image.  Reloc sections are assigned file
  1978.    positions in assign_file_positions_for_relocs, which is called by
  1979.    write_object_contents and final_link.
  1980.  
  1981.    If DOSYMS is false, we do not assign file positions for the symbol
  1982.    table or the string table.  */
  1983.  
  1984. static boolean
  1985. assign_file_positions_except_relocs (abfd, dosyms)
  1986.      bfd *abfd;
  1987.      boolean dosyms;
  1988. {
  1989.   struct elf_obj_tdata * const tdata = elf_tdata (abfd);
  1990.   Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
  1991.   Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
  1992.   file_ptr off;
  1993.  
  1994.   /* Start after the ELF header.  */
  1995.   off = i_ehdrp->e_ehsize;
  1996.  
  1997.   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
  1998.     {
  1999.       Elf_Internal_Shdr **hdrpp;
  2000.       unsigned int i;
  2001.  
  2002.       /* We are not creating an executable, which means that we are
  2003.      not creating a program header, and that the actual order of
  2004.      the sections in the file is unimportant.  */
  2005.       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
  2006.     {
  2007.       Elf_Internal_Shdr *hdr;
  2008.  
  2009.       hdr = *hdrpp;
  2010.       if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
  2011.         {
  2012.           hdr->sh_offset = -1;
  2013.           continue;
  2014.         }
  2015.       if (! dosyms
  2016.           && (i == tdata->symtab_section
  2017.           || i == tdata->strtab_section))
  2018.         {
  2019.           hdr->sh_offset = -1;
  2020.           continue;
  2021.         }
  2022.       
  2023.       off = assign_file_position_for_section (hdr, off, true);
  2024.     }
  2025.     }
  2026.   else
  2027.     {
  2028.       file_ptr phdr_off;
  2029.       bfd_size_type phdr_size;
  2030.       bfd_vma maxpagesize;
  2031.       Elf_Internal_Shdr **hdrpp;
  2032.       unsigned int i;
  2033.       Elf_Internal_Shdr *first;
  2034.       file_ptr phdr_map;
  2035.  
  2036.       /* We are creating an executable.  We must create a program
  2037.      header.  We can't actually create the program header until we
  2038.      have set the file positions for the sections, but we can
  2039.      figure out how big it is going to be.  */
  2040.       off = align_file_position (off);
  2041.       phdr_size = get_program_header_size (abfd);
  2042.       if (phdr_size == (file_ptr) -1)
  2043.     return false;
  2044.       phdr_off = off;
  2045.       off += phdr_size;
  2046.  
  2047.       maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
  2048.       if (maxpagesize == 0)
  2049.     maxpagesize = 1;
  2050.  
  2051.       /* FIXME: We might want to sort the sections on the sh_addr
  2052.      field here.  For now, we just assume that the linker will
  2053.      create the sections in an appropriate order.  */
  2054.  
  2055.       /* Assign file positions in two passes.  In the first pass, we
  2056.      assign a file position to every section which forms part of
  2057.      the executable image.  */
  2058.       first = NULL;
  2059.       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
  2060.     {
  2061.       Elf_Internal_Shdr *hdr;
  2062.  
  2063.       hdr = *hdrpp;
  2064.       if ((hdr->sh_flags & SHF_ALLOC) == 0)
  2065.         continue;
  2066.  
  2067.       if (first == NULL)
  2068.         first = hdr;
  2069.  
  2070.       /* The section VMA must equal the file position modulo the
  2071.          page size.  This is required by the program header.  */
  2072.       off += (hdr->sh_addr - off) % maxpagesize;
  2073.  
  2074.       off = assign_file_position_for_section (hdr, off, false);
  2075.     }
  2076.  
  2077.       /* Assign file positions to all the sections which do not form
  2078.      part of the loadable image, except for the relocs.  */
  2079.       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
  2080.     {
  2081.       Elf_Internal_Shdr *hdr;
  2082.  
  2083.       hdr = *hdrpp;
  2084.       if ((hdr->sh_flags & SHF_ALLOC) != 0)
  2085.         continue;
  2086.       if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
  2087.         {
  2088.           hdr->sh_offset = -1;
  2089.           continue;
  2090.         }
  2091.       if (! dosyms
  2092.           && (i == tdata->symtab_section
  2093.           || i == tdata->strtab_section))
  2094.         {
  2095.           hdr->sh_offset = -1;
  2096.           continue;
  2097.         }
  2098.  
  2099.       off = assign_file_position_for_section (hdr, off, true);
  2100.     }
  2101.  
  2102.       phdr_map = map_program_segments (abfd, phdr_off, first, phdr_size);
  2103.       if (phdr_map == (file_ptr) -1)
  2104.     return false;
  2105.       BFD_ASSERT (phdr_map <= phdr_off + phdr_size);
  2106.     }
  2107.  
  2108.   /* Place the section headers.  */
  2109.   off = align_file_position (off);
  2110.   i_ehdrp->e_shoff = off;
  2111.   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
  2112.  
  2113.   elf_tdata (abfd)->next_file_pos = off;
  2114.  
  2115.   return true;
  2116. }
  2117.  
  2118. static boolean
  2119. prep_headers (abfd)
  2120.      bfd *abfd;
  2121. {
  2122.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  2123.   Elf_Internal_Phdr *i_phdrp = 0;    /* Program header table, internal form */
  2124.   Elf_Internal_Shdr **i_shdrp;    /* Section header table, internal form */
  2125.   int count;
  2126.   struct strtab *shstrtab;
  2127.  
  2128.   i_ehdrp = elf_elfheader (abfd);
  2129.   i_shdrp = elf_elfsections (abfd);
  2130.  
  2131.   shstrtab = bfd_new_strtab (abfd);
  2132.   if (!shstrtab)
  2133.     return false;
  2134.  
  2135.   elf_shstrtab (abfd) = shstrtab;
  2136.  
  2137.   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
  2138.   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
  2139.   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
  2140.   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
  2141.  
  2142.   i_ehdrp->e_ident[EI_CLASS] = ELFCLASS;
  2143.   i_ehdrp->e_ident[EI_DATA] =
  2144.     abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
  2145.   i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
  2146.  
  2147.   for (count = EI_PAD; count < EI_NIDENT; count++)
  2148.     i_ehdrp->e_ident[count] = 0;
  2149.  
  2150.   if ((abfd->flags & DYNAMIC) != 0)
  2151.     i_ehdrp->e_type = ET_DYN;
  2152.   else if ((abfd->flags & EXEC_P) != 0)
  2153.     i_ehdrp->e_type = ET_EXEC;
  2154.   else
  2155.     i_ehdrp->e_type = ET_REL;
  2156.  
  2157.   switch (bfd_get_arch (abfd))
  2158.     {
  2159.     case bfd_arch_unknown:
  2160.       i_ehdrp->e_machine = EM_NONE;
  2161.       break;
  2162.     case bfd_arch_sparc:
  2163. #if ARCH_SIZE == 64
  2164.       i_ehdrp->e_machine = EM_SPARC64;
  2165. #else
  2166.       i_ehdrp->e_machine = EM_SPARC;
  2167. #endif
  2168.       break;
  2169.     case bfd_arch_i386:
  2170.       i_ehdrp->e_machine = EM_386;
  2171.       break;
  2172.     case bfd_arch_m68k:
  2173.       i_ehdrp->e_machine = EM_68K;
  2174.       break;
  2175.     case bfd_arch_m88k:
  2176.       i_ehdrp->e_machine = EM_88K;
  2177.       break;
  2178.     case bfd_arch_i860:
  2179.       i_ehdrp->e_machine = EM_860;
  2180.       break;
  2181.     case bfd_arch_mips:    /* MIPS Rxxxx */
  2182.       i_ehdrp->e_machine = EM_MIPS;    /* only MIPS R3000 */
  2183.       break;
  2184.     case bfd_arch_hppa:
  2185.       i_ehdrp->e_machine = EM_PARISC;
  2186.       break;
  2187.     case bfd_arch_powerpc:
  2188.       i_ehdrp->e_machine = EM_CYGNUS_POWERPC;
  2189.       break;
  2190.       /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
  2191.     default:
  2192.       i_ehdrp->e_machine = EM_NONE;
  2193.     }
  2194.   i_ehdrp->e_version = EV_CURRENT;
  2195.   i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr);
  2196.  
  2197.   /* no program header, for now. */
  2198.   i_ehdrp->e_phoff = 0;
  2199.   i_ehdrp->e_phentsize = 0;
  2200.   i_ehdrp->e_phnum = 0;
  2201.  
  2202.   /* each bfd section is section header entry */
  2203.   i_ehdrp->e_entry = bfd_get_start_address (abfd);
  2204.   i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
  2205.  
  2206.   /* if we're building an executable, we'll need a program header table */
  2207.   if (abfd->flags & EXEC_P)
  2208.     {
  2209.       /* it all happens later */
  2210. #if 0
  2211.       i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
  2212.  
  2213.       /* elf_build_phdrs() returns a (NULL-terminated) array of
  2214.      Elf_Internal_Phdrs */
  2215.       i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
  2216.       i_ehdrp->e_phoff = outbase;
  2217.       outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
  2218. #endif
  2219.     }
  2220.   else
  2221.     {
  2222.       i_ehdrp->e_phentsize = 0;
  2223.       i_phdrp = 0;
  2224.       i_ehdrp->e_phoff = 0;
  2225.     }
  2226.  
  2227.   elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
  2228.                                 ".symtab");
  2229.   elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
  2230.                                 ".strtab");
  2231.   elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
  2232.                                   ".shstrtab");
  2233.   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
  2234.       || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
  2235.       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
  2236.     return false;
  2237.  
  2238.   return true;
  2239. }
  2240.  
  2241. static boolean
  2242. swap_out_syms (abfd)
  2243.      bfd *abfd;
  2244. {
  2245.   if (!elf_map_symbols (abfd))
  2246.     return false;
  2247.  
  2248.   /* Dump out the symtabs. */
  2249.   {
  2250.     int symcount = bfd_get_symcount (abfd);
  2251.     asymbol **syms = bfd_get_outsymbols (abfd);
  2252.     struct strtab *stt = bfd_new_strtab (abfd);
  2253.     Elf_Internal_Shdr *symtab_hdr;
  2254.     Elf_Internal_Shdr *symstrtab_hdr;
  2255.     Elf_External_Sym *outbound_syms;
  2256.     int idx;
  2257.  
  2258.     if (!stt)
  2259.       return false;
  2260.     symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2261.     symtab_hdr->sh_type = SHT_SYMTAB;
  2262.     symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
  2263.     symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
  2264.     symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
  2265.     symtab_hdr->sh_addralign = FILE_ALIGN;
  2266.  
  2267.     symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
  2268.     symstrtab_hdr->sh_type = SHT_STRTAB;
  2269.  
  2270.     outbound_syms = (Elf_External_Sym *)
  2271.       bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym));
  2272.     if (!outbound_syms)
  2273.       {
  2274.     bfd_set_error (bfd_error_no_memory);
  2275.     return false;
  2276.       }
  2277.     /* now generate the data (for "contents") */
  2278.     {
  2279.       /* Fill in zeroth symbol and swap it out.  */
  2280.       Elf_Internal_Sym sym;
  2281.       sym.st_name = 0;
  2282.       sym.st_value = 0;
  2283.       sym.st_size = 0;
  2284.       sym.st_info = 0;
  2285.       sym.st_other = 0;
  2286.       sym.st_shndx = SHN_UNDEF;
  2287.       elf_swap_symbol_out (abfd, &sym, outbound_syms);
  2288.     }
  2289.     for (idx = 0; idx < symcount; idx++)
  2290.       {
  2291.     Elf_Internal_Sym sym;
  2292.     bfd_vma value = syms[idx]->value;
  2293.     elf_symbol_type *type_ptr;
  2294.  
  2295.     if (syms[idx]->flags & BSF_SECTION_SYM)
  2296.       /* Section symbols have no names.  */
  2297.       sym.st_name = 0;
  2298.     else
  2299.       {
  2300.         sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
  2301.         if (sym.st_name == (unsigned long) -1)
  2302.           return false;
  2303.       }
  2304.  
  2305.     type_ptr = elf_symbol_from (abfd, syms[idx]);
  2306.  
  2307.     if (bfd_is_com_section (syms[idx]->section))
  2308.       {
  2309.         /* ELF common symbols put the alignment into the `value' field,
  2310.            and the size into the `size' field.  This is backwards from
  2311.            how BFD handles it, so reverse it here.  */
  2312.         sym.st_size = value;
  2313.         if (type_ptr == NULL
  2314.         || type_ptr->internal_elf_sym.st_value == 0)
  2315.           sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
  2316.         else
  2317.           sym.st_value = type_ptr->internal_elf_sym.st_value;
  2318.         sym.st_shndx = elf_section_from_bfd_section (abfd,
  2319.                              syms[idx]->section);
  2320.       }
  2321.     else
  2322.       {
  2323.         asection *sec = syms[idx]->section;
  2324.         int shndx;
  2325.  
  2326.         if (sec->output_section)
  2327.           {
  2328.         value += sec->output_offset;
  2329.         sec = sec->output_section;
  2330.           }
  2331.         value += sec->vma;
  2332.         sym.st_value = value;
  2333.         sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
  2334.         sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec);
  2335.         if (shndx == -1)
  2336.           {
  2337.         asection *sec2;
  2338.         /* Writing this would be a hell of a lot easier if we had
  2339.            some decent documentation on bfd, and knew what to expect
  2340.            of the library, and what to demand of applications.  For
  2341.            example, it appears that `objcopy' might not set the
  2342.            section of a symbol to be a section that is actually in
  2343.            the output file.  */
  2344.         sec2 = bfd_get_section_by_name (abfd, sec->name);
  2345.         BFD_ASSERT (sec2 != 0);
  2346.         sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2);
  2347.         BFD_ASSERT (shndx != -1);
  2348.           }
  2349.       }
  2350.  
  2351.     if (bfd_is_com_section (syms[idx]->section))
  2352.       sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
  2353.     else if (bfd_is_und_section (syms[idx]->section))
  2354.       sym.st_info = ELF_ST_INFO (STB_GLOBAL,
  2355.                      ((syms[idx]->flags & BSF_FUNCTION)
  2356.                       ? STT_FUNC
  2357.                       : STT_NOTYPE));
  2358.     else if (syms[idx]->flags & BSF_SECTION_SYM)
  2359.       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  2360.     else if (syms[idx]->flags & BSF_FILE)
  2361.       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  2362.     else
  2363.       {
  2364.         int bind = STB_LOCAL;
  2365.         int type = STT_OBJECT;
  2366.         unsigned int flags = syms[idx]->flags;
  2367.  
  2368.         if (flags & BSF_LOCAL)
  2369.           bind = STB_LOCAL;
  2370.         else if (flags & BSF_WEAK)
  2371.           bind = STB_WEAK;
  2372.         else if (flags & BSF_GLOBAL)
  2373.           bind = STB_GLOBAL;
  2374.  
  2375.         if (flags & BSF_FUNCTION)
  2376.           type = STT_FUNC;
  2377.  
  2378.         sym.st_info = ELF_ST_INFO (bind, type);
  2379.       }
  2380.  
  2381.     sym.st_other = 0;
  2382.     elf_swap_symbol_out (abfd, &sym,
  2383.                  (outbound_syms
  2384.                   + elf_sym_extra (abfd)[idx].elf_sym_num));
  2385.       }
  2386.  
  2387.     symtab_hdr->contents = (PTR) outbound_syms;
  2388.     symstrtab_hdr->contents = (PTR) stt->tab;
  2389.     symstrtab_hdr->sh_size = stt->length;
  2390.     symstrtab_hdr->sh_type = SHT_STRTAB;
  2391.  
  2392.     symstrtab_hdr->sh_flags = 0;
  2393.     symstrtab_hdr->sh_addr = 0;
  2394.     symstrtab_hdr->sh_entsize = 0;
  2395.     symstrtab_hdr->sh_link = 0;
  2396.     symstrtab_hdr->sh_info = 0;
  2397.     symstrtab_hdr->sh_addralign = 1;
  2398.     symstrtab_hdr->size = 0;
  2399.   }
  2400.  
  2401.   return true;
  2402. }
  2403.  
  2404. static boolean
  2405. write_shdrs_and_ehdr (abfd)
  2406.      bfd *abfd;
  2407. {
  2408.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  2409.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  2410.   Elf_External_Shdr *x_shdrp;    /* Section header table, external form */
  2411.   Elf_Internal_Shdr **i_shdrp;    /* Section header table, internal form */
  2412.   unsigned int count;
  2413.   struct strtab *shstrtab;
  2414.  
  2415.   i_ehdrp = elf_elfheader (abfd);
  2416.   i_shdrp = elf_elfsections (abfd);
  2417.   shstrtab = elf_shstrtab (abfd);
  2418.  
  2419.   /* swap the header before spitting it out... */
  2420.  
  2421. #if DEBUG & 1
  2422.   elf_debug_file (i_ehdrp);
  2423. #endif
  2424.   elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
  2425.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  2426.       || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
  2427.       != sizeof (x_ehdr)))
  2428.     return false;
  2429.  
  2430.   /* at this point we've concocted all the ELF sections... */
  2431.   x_shdrp = (Elf_External_Shdr *)
  2432.     bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
  2433.   if (!x_shdrp)
  2434.     {
  2435.       bfd_set_error (bfd_error_no_memory);
  2436.       return false;
  2437.     }
  2438.  
  2439.   for (count = 0; count < i_ehdrp->e_shnum; count++)
  2440.     {
  2441. #if DEBUG & 2
  2442.       elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
  2443.              i_shdrp[count]);
  2444. #endif
  2445.       elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
  2446.     }
  2447.   if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
  2448.       || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
  2449.       != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
  2450.     return false;
  2451.  
  2452.   /* need to dump the string table too... */
  2453.  
  2454.   return true;
  2455. }
  2456.  
  2457. /* Assign file positions for all the reloc sections which are not part
  2458.    of the loadable file image.  */
  2459.  
  2460. static void
  2461. assign_file_positions_for_relocs (abfd)
  2462.      bfd *abfd;
  2463. {
  2464.   file_ptr off;
  2465.   unsigned int i;
  2466.   Elf_Internal_Shdr **shdrpp;
  2467.  
  2468.   off = elf_tdata (abfd)->next_file_pos;
  2469.  
  2470.   for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
  2471.        i < elf_elfheader (abfd)->e_shnum;
  2472.        i++, shdrpp++)
  2473.     {
  2474.       Elf_Internal_Shdr *shdrp;
  2475.  
  2476.       shdrp = *shdrpp;
  2477.       if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
  2478.       && shdrp->sh_offset == -1)
  2479.     off = assign_file_position_for_section (shdrp, off, true);
  2480.     }
  2481.  
  2482.   elf_tdata (abfd)->next_file_pos = off;
  2483. }
  2484.  
  2485. boolean
  2486. NAME(bfd_elf,write_object_contents) (abfd)
  2487.      bfd *abfd;
  2488. {
  2489.   struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2490.   Elf_Internal_Ehdr *i_ehdrp;
  2491.   Elf_Internal_Shdr **i_shdrp;
  2492.   unsigned int count;
  2493.  
  2494.   if (! abfd->output_has_begun
  2495.       && ! elf_compute_section_file_positions (abfd,
  2496.                            (struct bfd_link_info *) NULL))
  2497.     return false;
  2498.  
  2499.   i_shdrp = elf_elfsections (abfd);
  2500.   i_ehdrp = elf_elfheader (abfd);
  2501.  
  2502.   bfd_map_over_sections (abfd, write_relocs, (PTR) 0);
  2503.   assign_file_positions_for_relocs (abfd);
  2504.  
  2505.   /* After writing the headers, we need to write the sections too... */
  2506.   for (count = 1; count < i_ehdrp->e_shnum; count++)
  2507.     {
  2508.       if (bed->elf_backend_section_processing)
  2509.     (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
  2510.       if (i_shdrp[count]->contents)
  2511.     {
  2512.       if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
  2513.           || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
  2514.                  1, abfd)
  2515.           != i_shdrp[count]->sh_size))
  2516.         return false;
  2517.     }
  2518.     }
  2519.  
  2520.   if (bed->elf_backend_final_write_processing)
  2521.     (*bed->elf_backend_final_write_processing) (abfd,
  2522.                         elf_tdata (abfd)->linker);
  2523.  
  2524.   return write_shdrs_and_ehdr (abfd);
  2525. }
  2526.  
  2527. /* Given an index of a section, retrieve a pointer to it.  Note
  2528.    that for our purposes, sections are indexed by {1, 2, ...} with
  2529.    0 being an illegal index. */
  2530.  
  2531. /* In the original, each ELF section went into exactly one BFD
  2532.    section. This doesn't really make sense, so we need a real mapping.
  2533.    The mapping has to hide in the Elf_Internal_Shdr since asection
  2534.    doesn't have anything like a tdata field... */
  2535.  
  2536. static asection *
  2537. section_from_elf_index (abfd, index)
  2538.      bfd *abfd;
  2539.      unsigned int index;
  2540. {
  2541.   /* @@ Is bfd_com_section_ptr really correct in all the places it could
  2542.      be returned from this routine?  */
  2543.  
  2544.   if (index == SHN_ABS)
  2545.     return bfd_com_section_ptr;    /* not abs? */
  2546.   if (index == SHN_COMMON)
  2547.     return bfd_com_section_ptr;
  2548.  
  2549.   if (index >= elf_elfheader (abfd)->e_shnum)
  2550.     return NULL;
  2551.  
  2552.   {
  2553.     Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index];
  2554.  
  2555.     switch (hdr->sh_type)
  2556.       {
  2557.     /* ELF sections that map to BFD sections */
  2558.       case SHT_PROGBITS:
  2559.       case SHT_NOBITS:
  2560.       case SHT_HASH:
  2561.       case SHT_DYNAMIC:
  2562.     if (hdr->rawdata == NULL)
  2563.       {
  2564.         if (! bfd_section_from_shdr (abfd, index))
  2565.           return NULL;
  2566.       }
  2567.     return (struct sec *) hdr->rawdata;
  2568.  
  2569.       default:
  2570.     return bfd_abs_section_ptr;
  2571.       }
  2572.   }
  2573. }
  2574.  
  2575. /* given a section, search the header to find them... */
  2576. static int
  2577. elf_section_from_bfd_section (abfd, asect)
  2578.      bfd *abfd;
  2579.      struct sec *asect;
  2580. {
  2581.   Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
  2582.   int index;
  2583.   Elf_Internal_Shdr *hdr;
  2584.   int maxindex = elf_elfheader (abfd)->e_shnum;
  2585.  
  2586.   if (asect->owner == NULL)
  2587.     {
  2588.       if (bfd_is_abs_section (asect))
  2589.     return SHN_ABS;
  2590.       if (bfd_is_com_section (asect))
  2591.     return SHN_COMMON;
  2592.       if (bfd_is_und_section (asect))
  2593.     return SHN_UNDEF;
  2594.       return -1;
  2595.     }
  2596.  
  2597.   BFD_ASSERT (asect->owner == abfd);
  2598.  
  2599.   for (index = 0; index < maxindex; index++)
  2600.     {
  2601.       hdr = i_shdrp[index];
  2602.       switch (hdr->sh_type)
  2603.     {
  2604.       /* ELF sections that map to BFD sections */
  2605.     case SHT_PROGBITS:
  2606.     case SHT_NOBITS:
  2607.     case SHT_NOTE:
  2608.     case SHT_HASH:
  2609.     case SHT_DYNAMIC:
  2610.     case SHT_DYNSYM:
  2611.     case SHT_SYMTAB:
  2612.       if (hdr->rawdata)
  2613.         {
  2614.           if (((struct sec *) (hdr->rawdata)) == asect)
  2615.         return index;
  2616.         }
  2617.       break;
  2618.  
  2619.     case SHT_REL:
  2620.     case SHT_RELA:
  2621.       /* We sometimes map a reloc section to a BFD section.  */
  2622.       if (hdr->sh_link != elf_onesymtab (abfd)
  2623.           && (asection *) hdr->rawdata == asect)
  2624.         return index;
  2625.       break;
  2626.  
  2627.     case SHT_STRTAB:
  2628.       /* We map most string tables to BFD sections.  */
  2629.       if (index != elf_elfheader (abfd)->e_shstrndx
  2630.           && index != elf_onesymtab (abfd)
  2631.           && (asection *) hdr->rawdata == asect)
  2632.         return index;
  2633.  
  2634.       /* FALL THROUGH */
  2635.     default:
  2636.       {
  2637.         struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2638.  
  2639.         if (bed->elf_backend_section_from_bfd_section)
  2640.           {
  2641.         int retval;
  2642.  
  2643.         retval = index;
  2644.         if ((*bed->elf_backend_section_from_bfd_section)
  2645.             (abfd, hdr, asect, &retval))
  2646.           return retval;
  2647.           }
  2648.       }
  2649.       break;
  2650.     }
  2651.     }
  2652.   return -1;
  2653. }
  2654.  
  2655. /* given a symbol, return the bfd index for that symbol.  */
  2656. static int
  2657. elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
  2658.      bfd *abfd;
  2659.      struct symbol_cache_entry **asym_ptr_ptr;
  2660. {
  2661.   struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
  2662.   int idx;
  2663.   flagword flags = asym_ptr->flags;
  2664.  
  2665.   /* When gas creates relocations against local labels, it creates its
  2666.      own symbol for the section, but does put the symbol into the
  2667.      symbol chain, so udata is 0.  When the linker is generating
  2668.      relocatable output, this section symbol may be for one of the
  2669.      input sections rather than the output section.  */
  2670.   if (asym_ptr->udata == (PTR) 0
  2671.       && (flags & BSF_SECTION_SYM)
  2672.       && asym_ptr->section)
  2673.     {
  2674.       int indx;
  2675.  
  2676.       if (asym_ptr->section->output_section != NULL)
  2677.     indx = asym_ptr->section->output_section->index;
  2678.       else
  2679.     indx = asym_ptr->section->index;
  2680.       if (elf_section_syms (abfd)[indx])
  2681.     asym_ptr->udata = elf_section_syms (abfd)[indx]->udata;
  2682.     }
  2683.  
  2684.   if (asym_ptr->udata)
  2685.     idx = ((Elf_Sym_Extra *) asym_ptr->udata)->elf_sym_num;
  2686.   else
  2687.     {
  2688.       abort ();
  2689.     }
  2690.  
  2691. #if DEBUG & 4
  2692.   {
  2693.  
  2694.     fprintf (stderr,
  2695.          "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
  2696.      (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
  2697.     fflush (stderr);
  2698.   }
  2699. #endif
  2700.  
  2701.   return idx;
  2702. }
  2703.  
  2704. static long
  2705. elf_slurp_symbol_table (abfd, symptrs, dynamic)
  2706.      bfd *abfd;
  2707.      asymbol **symptrs;        /* Buffer for generated bfd symbols */
  2708.      boolean dynamic;
  2709. {
  2710.   Elf_Internal_Shdr *hdr;
  2711.   long symcount;        /* Number of external ELF symbols */
  2712.   elf_symbol_type *sym;        /* Pointer to current bfd symbol */
  2713.   elf_symbol_type *symbase;    /* Buffer for generated bfd symbols */
  2714.   Elf_Internal_Sym i_sym;
  2715.   Elf_External_Sym *x_symp = NULL;
  2716.  
  2717.   /* Read each raw ELF symbol, converting from external ELF form to
  2718.      internal ELF form, and then using the information to create a
  2719.      canonical bfd symbol table entry.
  2720.  
  2721.      Note that we allocate the initial bfd canonical symbol buffer
  2722.      based on a one-to-one mapping of the ELF symbols to canonical
  2723.      symbols.  We actually use all the ELF symbols, so there will be no
  2724.      space left over at the end.  When we have all the symbols, we
  2725.      build the caller's pointer vector. */
  2726.  
  2727.   if (dynamic)
  2728.     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  2729.   else
  2730.     hdr = &elf_tdata (abfd)->symtab_hdr;
  2731.   if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
  2732.     return -1;
  2733.  
  2734.   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  2735.  
  2736.   if (symcount == 0)
  2737.     sym = symbase = NULL;
  2738.   else
  2739.     {
  2740.       long i;
  2741.  
  2742.       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
  2743.     return -1;
  2744.  
  2745.       symbase = ((elf_symbol_type *)
  2746.          bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
  2747.       if (symbase == (elf_symbol_type *) NULL)
  2748.     {
  2749.       bfd_set_error (bfd_error_no_memory);
  2750.       return -1;
  2751.     }
  2752.       sym = symbase;
  2753.  
  2754.       /* Temporarily allocate room for the raw ELF symbols.  */
  2755.       x_symp = ((Elf_External_Sym *)
  2756.         malloc (symcount * sizeof (Elf_External_Sym)));
  2757.       if (x_symp == NULL && symcount != 0)
  2758.     {
  2759.       bfd_set_error (bfd_error_no_memory);
  2760.       goto error_return;
  2761.     }
  2762.  
  2763.       if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
  2764.       != symcount * sizeof (Elf_External_Sym))
  2765.     goto error_return;
  2766.       /* Skip first symbol, which is a null dummy.  */
  2767.       for (i = 1; i < symcount; i++)
  2768.     {
  2769.       elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
  2770.       memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
  2771. #ifdef ELF_KEEP_EXTSYM
  2772.       memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
  2773. #endif
  2774.       sym->symbol.the_bfd = abfd;
  2775.  
  2776.       sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link,
  2777.                               i_sym.st_name);
  2778.  
  2779.       sym->symbol.value = i_sym.st_value;
  2780.  
  2781.       if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE)
  2782.         {
  2783.           sym->symbol.section = section_from_elf_index (abfd,
  2784.                                 i_sym.st_shndx);
  2785.           if (sym->symbol.section == NULL)
  2786.         {
  2787.           /* This symbol is in a section for which we did not
  2788.              create a BFD section.  Just use bfd_abs_section,
  2789.              although it is wrong.  FIXME.  */
  2790.           sym->symbol.section = bfd_abs_section_ptr;
  2791.         }
  2792.         }
  2793.       else if (i_sym.st_shndx == SHN_ABS)
  2794.         {
  2795.           sym->symbol.section = bfd_abs_section_ptr;
  2796.         }
  2797.       else if (i_sym.st_shndx == SHN_COMMON)
  2798.         {
  2799.           sym->symbol.section = bfd_com_section_ptr;
  2800.           /* Elf puts the alignment into the `value' field, and
  2801.          the size into the `size' field.  BFD wants to see the
  2802.          size in the value field, and doesn't care (at the
  2803.          moment) about the alignment.  */
  2804.           sym->symbol.value = i_sym.st_size;
  2805.         }
  2806.       else if (i_sym.st_shndx == SHN_UNDEF)
  2807.         {
  2808.           sym->symbol.section = bfd_und_section_ptr;
  2809.         }
  2810.       else
  2811.         sym->symbol.section = bfd_abs_section_ptr;
  2812.  
  2813.       sym->symbol.value -= sym->symbol.section->vma;
  2814.  
  2815.       switch (ELF_ST_BIND (i_sym.st_info))
  2816.         {
  2817.         case STB_LOCAL:
  2818.           sym->symbol.flags |= BSF_LOCAL;
  2819.           break;
  2820.         case STB_GLOBAL:
  2821.           sym->symbol.flags |= BSF_GLOBAL;
  2822.           break;
  2823.         case STB_WEAK:
  2824.           sym->symbol.flags |= BSF_WEAK;
  2825.           break;
  2826.         }
  2827.  
  2828.       switch (ELF_ST_TYPE (i_sym.st_info))
  2829.         {
  2830.         case STT_SECTION:
  2831.           sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
  2832.           break;
  2833.         case STT_FILE:
  2834.           sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
  2835.           break;
  2836.         case STT_FUNC:
  2837.           sym->symbol.flags |= BSF_FUNCTION;
  2838.           break;
  2839.         }
  2840.  
  2841.       if (dynamic)
  2842.         sym->symbol.flags |= BSF_DYNAMIC;
  2843.  
  2844.       /* Do some backend-specific processing on this symbol.  */
  2845.       {
  2846.         struct elf_backend_data *ebd = get_elf_backend_data (abfd);
  2847.         if (ebd->elf_backend_symbol_processing)
  2848.           (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
  2849.       }
  2850.  
  2851.       sym++;
  2852.     }
  2853.     }
  2854.  
  2855.   /* Do some backend-specific processing on this symbol table.  */
  2856.   {
  2857.     struct elf_backend_data *ebd = get_elf_backend_data (abfd);
  2858.     if (ebd->elf_backend_symbol_table_processing)
  2859.       (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
  2860.   }
  2861.  
  2862.   /* We rely on the zalloc to clear out the final symbol entry.  */
  2863.  
  2864.   symcount = sym - symbase;
  2865.  
  2866.   /* Fill in the user's symbol pointer vector if needed.  */
  2867.   if (symptrs)
  2868.     {
  2869.       long l = symcount;
  2870.  
  2871.       sym = symbase;
  2872.       while (l-- > 0)
  2873.     {
  2874.       *symptrs++ = &sym->symbol;
  2875.       sym++;
  2876.     }
  2877.       *symptrs = 0;        /* Final null pointer */
  2878.     }
  2879.  
  2880.   if (x_symp != NULL)
  2881.     free (x_symp);
  2882.   return symcount;
  2883. error_return:
  2884.   if (x_symp != NULL)
  2885.     free (x_symp);
  2886.   return -1;
  2887. }
  2888.  
  2889. /* Return the number of bytes required to hold the symtab vector.
  2890.  
  2891.    Note that we base it on the count plus 1, since we will null terminate
  2892.    the vector allocated based on this size.  However, the ELF symbol table
  2893.    always has a dummy entry as symbol #0, so it ends up even.  */
  2894.  
  2895. long
  2896. elf_get_symtab_upper_bound (abfd)
  2897.      bfd *abfd;
  2898. {
  2899.   long symcount;
  2900.   long symtab_size;
  2901.   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
  2902.  
  2903.   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  2904.   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
  2905.  
  2906.   return symtab_size;
  2907. }
  2908.  
  2909. long
  2910. elf_get_dynamic_symtab_upper_bound (abfd)
  2911.      bfd *abfd;
  2912. {
  2913.   long symcount;
  2914.   long symtab_size;
  2915.   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  2916.  
  2917.   if (elf_dynsymtab (abfd) == 0)
  2918.     {
  2919.       bfd_set_error (bfd_error_invalid_operation);
  2920.       return -1;
  2921.     }
  2922.  
  2923.   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  2924.   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
  2925.  
  2926.   return symtab_size;
  2927. }
  2928.  
  2929. long
  2930. elf_get_reloc_upper_bound (abfd, asect)
  2931.      bfd *abfd;
  2932.      sec_ptr asect;
  2933. {
  2934.   return (asect->reloc_count + 1) * sizeof (arelent *);
  2935. }
  2936.  
  2937. /* Read in and swap the external relocs.  */
  2938.  
  2939. static boolean
  2940. elf_slurp_reloc_table (abfd, asect, symbols)
  2941.      bfd *abfd;
  2942.      asection *asect;
  2943.      asymbol **symbols;
  2944. {
  2945.   struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
  2946.   struct bfd_elf_section_data * const d = elf_section_data (asect);
  2947.   PTR allocated = NULL;
  2948.   bfd_byte *native_relocs;
  2949.   arelent *relents;
  2950.   arelent *relent;
  2951.   unsigned int i;
  2952.   int entsize;
  2953.  
  2954.   if (asect->relocation != NULL
  2955.       || (asect->flags & SEC_RELOC) == 0
  2956.       || asect->reloc_count == 0)
  2957.     return true;
  2958.  
  2959.   BFD_ASSERT (asect->rel_filepos == d->rel_hdr.sh_offset
  2960.           && (asect->reloc_count
  2961.           == d->rel_hdr.sh_size / d->rel_hdr.sh_entsize));
  2962.  
  2963.   allocated = (PTR) malloc (d->rel_hdr.sh_size);
  2964.   if (allocated == NULL)
  2965.     {
  2966.       bfd_set_error (bfd_error_no_memory);
  2967.       goto error_return;
  2968.     }
  2969.  
  2970.   if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0
  2971.       || (bfd_read (allocated, 1, d->rel_hdr.sh_size, abfd)
  2972.       != d->rel_hdr.sh_size))
  2973.     goto error_return;
  2974.  
  2975.   native_relocs = (bfd_byte *) allocated;
  2976.  
  2977.   relents = ((arelent *)
  2978.          bfd_alloc (abfd, asect->reloc_count * sizeof (arelent)));
  2979.   if (relents == NULL)
  2980.     {
  2981.       bfd_set_error (bfd_error_no_memory);
  2982.       goto error_return;
  2983.     }
  2984.  
  2985.   entsize = d->rel_hdr.sh_entsize;
  2986.   BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
  2987.           || entsize == sizeof (Elf_External_Rela));
  2988.  
  2989.   for (i = 0, relent = relents;
  2990.        i < asect->reloc_count;
  2991.        i++, relent++, native_relocs += entsize)
  2992.     {
  2993.       Elf_Internal_Rela rela;
  2994.       Elf_Internal_Rel rel;
  2995.  
  2996.       if (entsize == sizeof (Elf_External_Rela))
  2997.     elf_swap_reloca_in (abfd, (Elf_External_Rela *) native_relocs, &rela);
  2998.       else
  2999.     {
  3000.       elf_swap_reloc_in (abfd, (Elf_External_Rel *) native_relocs, &rel);
  3001.       rela.r_offset = rel.r_offset;
  3002.       rela.r_info = rel.r_info;
  3003.       rela.r_addend = 0;
  3004.     }
  3005.  
  3006.       /* The address of an ELF reloc is section relative for an object
  3007.      file, and absolute for an executable file or shared library.
  3008.      The address of a BFD reloc is always section relative.  */
  3009.       if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
  3010.     relent->address = rela.r_offset;
  3011.       else
  3012.     relent->address = rela.r_offset - asect->vma;
  3013.  
  3014.       if (ELF_R_SYM (rela.r_info) == 0)
  3015.     relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  3016.       else
  3017.     {
  3018.       asymbol **ps, *s;
  3019.  
  3020.       ps = symbols + ELF_R_SYM (rela.r_info) - 1;
  3021.       s = *ps;
  3022.  
  3023.       /* Canonicalize ELF section symbols.  FIXME: Why?  */
  3024.       if ((s->flags & BSF_SECTION_SYM) == 0)
  3025.         relent->sym_ptr_ptr = ps;
  3026.       else
  3027.         relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
  3028.     }
  3029.  
  3030.       relent->addend = rela.r_addend;
  3031.  
  3032.       if (entsize == sizeof (Elf_External_Rela))
  3033.     (*ebd->elf_info_to_howto) (abfd, relent, &rela);
  3034.       else
  3035.     (*ebd->elf_info_to_howto_rel) (abfd, relent, &rel);
  3036.     }
  3037.  
  3038.   asect->relocation = relents;
  3039.  
  3040.   if (allocated != NULL)
  3041.     free (allocated);
  3042.  
  3043.   return true;
  3044.  
  3045.  error_return:
  3046.   if (allocated != NULL)
  3047.     free (allocated);
  3048.   return false;
  3049. }
  3050.  
  3051. #ifdef DEBUG
  3052. static void
  3053. elf_debug_section (str, num, hdr)
  3054.      char *str;
  3055.      int num;
  3056.      Elf_Internal_Shdr *hdr;
  3057. {
  3058.   fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
  3059.   fprintf (stderr,
  3060.        "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
  3061.        (long) hdr->sh_name,
  3062.        (long) hdr->sh_type,
  3063.        (long) hdr->sh_flags);
  3064.   fprintf (stderr,
  3065.        "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
  3066.        (long) hdr->sh_addr,
  3067.        (long) hdr->sh_offset,
  3068.        (long) hdr->sh_size);
  3069.   fprintf (stderr,
  3070.        "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
  3071.        (long) hdr->sh_link,
  3072.        (long) hdr->sh_info,
  3073.        (long) hdr->sh_addralign);
  3074.   fprintf (stderr, "sh_entsize   = %ld\n",
  3075.        (long) hdr->sh_entsize);
  3076.   fprintf (stderr, "rawdata      = 0x%.8lx\n", (long) hdr->rawdata);
  3077.   fprintf (stderr, "contents     = 0x%.8lx\n", (long) hdr->contents);
  3078.   fprintf (stderr, "size         = %ld\n", (long) hdr->size);
  3079.   fflush (stderr);
  3080. }
  3081.  
  3082. static void
  3083. elf_debug_file (ehdrp)
  3084.      Elf_Internal_Ehdr *ehdrp;
  3085. {
  3086.   fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
  3087.   fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
  3088.   fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
  3089.   fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
  3090.   fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
  3091.   fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
  3092.   fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
  3093. }
  3094. #endif
  3095.  
  3096. /* Canonicalize the relocs.  */
  3097.  
  3098. long
  3099. elf_canonicalize_reloc (abfd, section, relptr, symbols)
  3100.      bfd *abfd;
  3101.      sec_ptr section;
  3102.      arelent **relptr;
  3103.      asymbol **symbols;
  3104. {
  3105.   arelent *tblptr;
  3106.   unsigned int i;
  3107.  
  3108.   if (! elf_slurp_reloc_table (abfd, section, symbols))
  3109.     return -1;
  3110.  
  3111.   tblptr = section->relocation;
  3112.   for (i = 0; i < section->reloc_count; i++)
  3113.     *relptr++ = tblptr++;
  3114.  
  3115.   *relptr = NULL;
  3116.  
  3117.   return section->reloc_count;
  3118. }
  3119.  
  3120. long
  3121. elf_get_symtab (abfd, alocation)
  3122.      bfd *abfd;
  3123.      asymbol **alocation;
  3124. {
  3125.   long symcount = elf_slurp_symbol_table (abfd, alocation, false);
  3126.  
  3127.   if (symcount >= 0)
  3128.     bfd_get_symcount (abfd) = symcount;
  3129.   return symcount;
  3130. }
  3131.  
  3132. long
  3133. elf_canonicalize_dynamic_symtab (abfd, alocation)
  3134.      bfd *abfd;
  3135.      asymbol **alocation;
  3136. {
  3137.   return elf_slurp_symbol_table (abfd, alocation, true);
  3138. }
  3139.  
  3140. asymbol *
  3141. elf_make_empty_symbol (abfd)
  3142.      bfd *abfd;
  3143. {
  3144.   elf_symbol_type *newsym;
  3145.  
  3146.   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
  3147.   if (!newsym)
  3148.     {
  3149.       bfd_set_error (bfd_error_no_memory);
  3150.       return NULL;
  3151.     }
  3152.   else
  3153.     {
  3154.       newsym->symbol.the_bfd = abfd;
  3155.       return &newsym->symbol;
  3156.     }
  3157. }
  3158.  
  3159. void
  3160. elf_get_symbol_info (ignore_abfd, symbol, ret)
  3161.      bfd *ignore_abfd;
  3162.      asymbol *symbol;
  3163.      symbol_info *ret;
  3164. {
  3165.   bfd_symbol_info (symbol, ret);
  3166. }
  3167.  
  3168. alent *
  3169. elf_get_lineno (ignore_abfd, symbol)
  3170.      bfd *ignore_abfd;
  3171.      asymbol *symbol;
  3172. {
  3173.   fprintf (stderr, "elf_get_lineno unimplemented\n");
  3174.   fflush (stderr);
  3175.   BFD_FAIL ();
  3176.   return NULL;
  3177. }
  3178.  
  3179. boolean
  3180. elf_set_arch_mach (abfd, arch, machine)
  3181.      bfd *abfd;
  3182.      enum bfd_architecture arch;
  3183.      unsigned long machine;
  3184. {
  3185.   /* If this isn't the right architecture for this backend, and this
  3186.      isn't the generic backend, fail.  */
  3187.   if (arch != get_elf_backend_data (abfd)->arch
  3188.       && arch != bfd_arch_unknown
  3189.       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
  3190.     return false;
  3191.  
  3192.   return bfd_default_set_arch_mach (abfd, arch, machine);
  3193. }
  3194.  
  3195. boolean
  3196. elf_find_nearest_line (abfd,
  3197.                section,
  3198.                symbols,
  3199.                offset,
  3200.                filename_ptr,
  3201.                functionname_ptr,
  3202.                line_ptr)
  3203.      bfd *abfd;
  3204.      asection *section;
  3205.      asymbol **symbols;
  3206.      bfd_vma offset;
  3207.      CONST char **filename_ptr;
  3208.      CONST char **functionname_ptr;
  3209.      unsigned int *line_ptr;
  3210. {
  3211.   return false;
  3212. }
  3213.  
  3214. int
  3215. elf_sizeof_headers (abfd, reloc)
  3216.      bfd *abfd;
  3217.      boolean reloc;
  3218. {
  3219.   int ret;
  3220.  
  3221.   ret = sizeof (Elf_External_Ehdr);
  3222.   if (! reloc)
  3223.     ret += get_program_header_size (abfd);
  3224.   return ret;
  3225. }
  3226.  
  3227. boolean
  3228. elf_set_section_contents (abfd, section, location, offset, count)
  3229.      bfd *abfd;
  3230.      sec_ptr section;
  3231.      PTR location;
  3232.      file_ptr offset;
  3233.      bfd_size_type count;
  3234. {
  3235.   Elf_Internal_Shdr *hdr;
  3236.  
  3237.   if (! abfd->output_has_begun
  3238.       && ! elf_compute_section_file_positions (abfd,
  3239.                            (struct bfd_link_info *) NULL))
  3240.     return false;
  3241.  
  3242.   hdr = &elf_section_data (section)->this_hdr;
  3243.  
  3244.   if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
  3245.     return false;
  3246.   if (bfd_write (location, 1, count, abfd) != count)
  3247.     return false;
  3248.  
  3249.   return true;
  3250. }
  3251.  
  3252. void
  3253. elf_no_info_to_howto (abfd, cache_ptr, dst)
  3254.      bfd *abfd;
  3255.      arelent *cache_ptr;
  3256.      Elf_Internal_Rela *dst;
  3257. {
  3258.   fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n");
  3259.   fflush (stderr);
  3260.   BFD_FAIL ();
  3261. }
  3262.  
  3263. void
  3264. elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
  3265.      bfd *abfd;
  3266.      arelent *cache_ptr;
  3267.      Elf_Internal_Rel *dst;
  3268. {
  3269.   fprintf (stderr, "elf REL relocation support for target machine unimplemented\n");
  3270.   fflush (stderr);
  3271.   BFD_FAIL ();
  3272. }
  3273.  
  3274.  
  3275. /* Core file support */
  3276.  
  3277. #ifdef HAVE_PROCFS        /* Some core file support requires host /proc files */
  3278. #include <sys/procfs.h>
  3279. #else
  3280. #define bfd_prstatus(abfd, descdata, descsz, filepos) true
  3281. #define bfd_fpregset(abfd, descdata, descsz, filepos) true
  3282. #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true
  3283. #endif
  3284.  
  3285. #ifdef HAVE_PROCFS
  3286.  
  3287. static boolean
  3288. bfd_prstatus (abfd, descdata, descsz, filepos)
  3289.      bfd *abfd;
  3290.      char *descdata;
  3291.      int descsz;
  3292.      long filepos;
  3293. {
  3294.   asection *newsect;
  3295.   prstatus_t *status = (prstatus_t *) 0;
  3296.  
  3297.   if (descsz == sizeof (prstatus_t))
  3298.     {
  3299.       newsect = bfd_make_section (abfd, ".reg");
  3300.       if (newsect == NULL)
  3301.     return false;
  3302.       newsect->_raw_size = sizeof (status->pr_reg);
  3303.       newsect->filepos = filepos + (long) &status->pr_reg;
  3304.       newsect->flags = SEC_HAS_CONTENTS;
  3305.       newsect->alignment_power = 2;
  3306.       if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
  3307.     {
  3308.       memcpy (core_prstatus (abfd), descdata, descsz);
  3309.     }
  3310.     }
  3311.   return true;
  3312. }
  3313.  
  3314. /* Stash a copy of the prpsinfo structure away for future use. */
  3315.  
  3316. static boolean
  3317. bfd_prpsinfo (abfd, descdata, descsz, filepos)
  3318.      bfd *abfd;
  3319.      char *descdata;
  3320.      int descsz;
  3321.      long filepos;
  3322. {
  3323.   if (descsz == sizeof (prpsinfo_t))
  3324.     {
  3325.       if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) == NULL)
  3326.     {
  3327.       bfd_set_error (bfd_error_no_memory);
  3328.       return false;
  3329.     }
  3330.       memcpy (core_prpsinfo (abfd), descdata, descsz);
  3331.     }
  3332.   return true;
  3333. }
  3334.  
  3335. static boolean
  3336. bfd_fpregset (abfd, descdata, descsz, filepos)
  3337.      bfd *abfd;
  3338.      char *descdata;
  3339.      int descsz;
  3340.      long filepos;
  3341. {
  3342.   asection *newsect;
  3343.  
  3344.   newsect = bfd_make_section (abfd, ".reg2");
  3345.   if (newsect == NULL)
  3346.     return false;
  3347.   newsect->_raw_size = descsz;
  3348.   newsect->filepos = filepos;
  3349.   newsect->flags = SEC_HAS_CONTENTS;
  3350.   newsect->alignment_power = 2;
  3351.   return true;
  3352. }
  3353.  
  3354. #endif /* HAVE_PROCFS */
  3355.  
  3356. /* Return a pointer to the args (including the command name) that were
  3357.    seen by the program that generated the core dump.  Note that for
  3358.    some reason, a spurious space is tacked onto the end of the args
  3359.    in some (at least one anyway) implementations, so strip it off if
  3360.    it exists. */
  3361.  
  3362. char *
  3363. elf_core_file_failing_command (abfd)
  3364.      bfd *abfd;
  3365. {
  3366. #ifdef HAVE_PROCFS
  3367.   if (core_prpsinfo (abfd))
  3368.     {
  3369.       prpsinfo_t *p = core_prpsinfo (abfd);
  3370.       char *scan = p->pr_psargs;
  3371.       while (*scan++)
  3372.     {;
  3373.     }
  3374.       scan -= 2;
  3375.       if ((scan > p->pr_psargs) && (*scan == ' '))
  3376.     {
  3377.       *scan = '\000';
  3378.     }
  3379.       return p->pr_psargs;
  3380.     }
  3381. #endif
  3382.   return NULL;
  3383. }
  3384.  
  3385. /* Return the number of the signal that caused the core dump.  Presumably,
  3386.    since we have a core file, we got a signal of some kind, so don't bother
  3387.    checking the other process status fields, just return the signal number.
  3388.    */
  3389.  
  3390. int
  3391. elf_core_file_failing_signal (abfd)
  3392.      bfd *abfd;
  3393. {
  3394. #ifdef HAVE_PROCFS
  3395.   if (core_prstatus (abfd))
  3396.     {
  3397.       return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
  3398.     }
  3399. #endif
  3400.   return -1;
  3401. }
  3402.  
  3403. /* Check to see if the core file could reasonably be expected to have
  3404.    come for the current executable file.  Note that by default we return
  3405.    true unless we find something that indicates that there might be a
  3406.    problem.
  3407.    */
  3408.  
  3409. boolean
  3410. elf_core_file_matches_executable_p (core_bfd, exec_bfd)
  3411.      bfd *core_bfd;
  3412.      bfd *exec_bfd;
  3413. {
  3414. #ifdef HAVE_PROCFS
  3415.   char *corename;
  3416.   char *execname;
  3417. #endif
  3418.  
  3419.   /* First, xvecs must match since both are ELF files for the same target. */
  3420.  
  3421.   if (core_bfd->xvec != exec_bfd->xvec)
  3422.     {
  3423.       bfd_set_error (bfd_error_system_call);
  3424.       return false;
  3425.     }
  3426.  
  3427. #ifdef HAVE_PROCFS
  3428.  
  3429.   /* If no prpsinfo, just return true.  Otherwise, grab the last component
  3430.      of the exec'd pathname from the prpsinfo. */
  3431.  
  3432.   if (core_prpsinfo (core_bfd))
  3433.     {
  3434.       corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname);
  3435.     }
  3436.   else
  3437.     {
  3438.       return true;
  3439.     }
  3440.  
  3441.   /* Find the last component of the executable pathname. */
  3442.  
  3443.   if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
  3444.     {
  3445.       execname++;
  3446.     }
  3447.   else
  3448.     {
  3449.       execname = (char *) exec_bfd->filename;
  3450.     }
  3451.  
  3452.   /* See if they match */
  3453.  
  3454.   return strcmp (execname, corename) ? false : true;
  3455.  
  3456. #else
  3457.  
  3458.   return true;
  3459.  
  3460. #endif /* HAVE_PROCFS */
  3461. }
  3462.  
  3463. /* ELF core files contain a segment of type PT_NOTE, that holds much of
  3464.    the information that would normally be available from the /proc interface
  3465.    for the process, at the time the process dumped core.  Currently this
  3466.    includes copies of the prstatus, prpsinfo, and fpregset structures.
  3467.  
  3468.    Since these structures are potentially machine dependent in size and
  3469.    ordering, bfd provides two levels of support for them.  The first level,
  3470.    available on all machines since it does not require that the host
  3471.    have /proc support or the relevant include files, is to create a bfd
  3472.    section for each of the prstatus, prpsinfo, and fpregset structures,
  3473.    without any interpretation of their contents.  With just this support,
  3474.    the bfd client will have to interpret the structures itself.  Even with
  3475.    /proc support, it might want these full structures for it's own reasons.
  3476.  
  3477.    In the second level of support, where HAVE_PROCFS is defined, bfd will
  3478.    pick apart the structures to gather some additional information that
  3479.    clients may want, such as the general register set, the name of the
  3480.    exec'ed file and its arguments, the signal (if any) that caused the
  3481.    core dump, etc.
  3482.  
  3483.    */
  3484.  
  3485. static boolean
  3486. elf_corefile_note (abfd, hdr)
  3487.      bfd *abfd;
  3488.      Elf_Internal_Phdr *hdr;
  3489. {
  3490.   Elf_External_Note *x_note_p;    /* Elf note, external form */
  3491.   Elf_Internal_Note i_note;    /* Elf note, internal form */
  3492.   char *buf = NULL;        /* Entire note segment contents */
  3493.   char *namedata;        /* Name portion of the note */
  3494.   char *descdata;        /* Descriptor portion of the note */
  3495.   char *sectname;        /* Name to use for new section */
  3496.   long filepos;            /* File offset to descriptor data */
  3497.   asection *newsect;
  3498.  
  3499.   if (hdr->p_filesz > 0
  3500.       && (buf = (char *) malloc (hdr->p_filesz)) != NULL
  3501.       && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
  3502.       && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
  3503.     {
  3504.       x_note_p = (Elf_External_Note *) buf;
  3505.       while ((char *) x_note_p < (buf + hdr->p_filesz))
  3506.     {
  3507.       i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
  3508.       i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
  3509.       i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
  3510.       namedata = x_note_p->name;
  3511.       descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
  3512.       filepos = hdr->p_offset + (descdata - buf);
  3513.       switch (i_note.type)
  3514.         {
  3515.         case NT_PRSTATUS:
  3516.           /* process descdata as prstatus info */
  3517.           if (! bfd_prstatus (abfd, descdata, i_note.descsz, filepos))
  3518.         return false;
  3519.           sectname = ".prstatus";
  3520.           break;
  3521.         case NT_FPREGSET:
  3522.           /* process descdata as fpregset info */
  3523.           if (! bfd_fpregset (abfd, descdata, i_note.descsz, filepos))
  3524.         return false;
  3525.           sectname = ".fpregset";
  3526.           break;
  3527.         case NT_PRPSINFO:
  3528.           /* process descdata as prpsinfo */
  3529.           if (! bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos))
  3530.         return false;
  3531.           sectname = ".prpsinfo";
  3532.           break;
  3533.         default:
  3534.           /* Unknown descriptor, just ignore it. */
  3535.           sectname = NULL;
  3536.           break;
  3537.         }
  3538.       if (sectname != NULL)
  3539.         {
  3540.           newsect = bfd_make_section (abfd, sectname);
  3541.           if (newsect == NULL)
  3542.         return false;
  3543.           newsect->_raw_size = i_note.descsz;
  3544.           newsect->filepos = filepos;
  3545.           newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  3546.           newsect->alignment_power = 2;
  3547.         }
  3548.       x_note_p = (Elf_External_Note *)
  3549.         (descdata + BFD_ALIGN (i_note.descsz, 4));
  3550.     }
  3551.     }
  3552.   if (buf != NULL)
  3553.     {
  3554.       free (buf);
  3555.     }
  3556.   else if (hdr->p_filesz > 0)
  3557.     {
  3558.       bfd_set_error (bfd_error_no_memory);
  3559.       return false;
  3560.     }
  3561.   return true;
  3562.  
  3563. }
  3564.  
  3565. /*  Core files are simply standard ELF formatted files that partition
  3566.     the file using the execution view of the file (program header table)
  3567.     rather than the linking view.  In fact, there is no section header
  3568.     table in a core file.
  3569.  
  3570.     The process status information (including the contents of the general
  3571.     register set) and the floating point register set are stored in a
  3572.     segment of type PT_NOTE.  We handcraft a couple of extra bfd sections
  3573.     that allow standard bfd access to the general registers (.reg) and the
  3574.     floating point registers (.reg2).
  3575.  
  3576.  */
  3577.  
  3578. const bfd_target *
  3579. elf_core_file_p (abfd)
  3580.      bfd *abfd;
  3581. {
  3582.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  3583.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  3584.   Elf_External_Phdr x_phdr;    /* Program header table entry, external form */
  3585.   Elf_Internal_Phdr *i_phdrp;    /* Program header table, internal form */
  3586.   unsigned int phindex;
  3587.   struct elf_backend_data *ebd;
  3588.  
  3589.   /* Read in the ELF header in external format.  */
  3590.  
  3591.   if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
  3592.     {
  3593.       if (bfd_get_error () != bfd_error_system_call)
  3594.     bfd_set_error (bfd_error_wrong_format);
  3595.       return NULL;
  3596.     }
  3597.  
  3598.   /* Now check to see if we have a valid ELF file, and one that BFD can
  3599.      make use of.  The magic number must match, the address size ('class')
  3600.      and byte-swapping must match our XVEC entry, and it must have a
  3601.      program header table (FIXME: See comments re segments at top of this
  3602.      file). */
  3603.  
  3604.   if (elf_file_p (&x_ehdr) == false)
  3605.     {
  3606.     wrong:
  3607.       bfd_set_error (bfd_error_wrong_format);
  3608.       return NULL;
  3609.     }
  3610.  
  3611.   /* FIXME, Check EI_VERSION here !  */
  3612.  
  3613.   {
  3614. #if ARCH_SIZE == 32
  3615.     int desired_address_size = ELFCLASS32;
  3616. #endif
  3617. #if ARCH_SIZE == 64
  3618.     int desired_address_size = ELFCLASS64;
  3619. #endif
  3620.  
  3621.     if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
  3622.       goto wrong;
  3623.   }
  3624.  
  3625.   /* Switch xvec to match the specified byte order.  */
  3626.   switch (x_ehdr.e_ident[EI_DATA])
  3627.     {
  3628.     case ELFDATA2MSB:        /* Big-endian */
  3629.       if (abfd->xvec->byteorder_big_p == false)
  3630.     goto wrong;
  3631.       break;
  3632.     case ELFDATA2LSB:        /* Little-endian */
  3633.       if (abfd->xvec->byteorder_big_p == true)
  3634.     goto wrong;
  3635.       break;
  3636.     case ELFDATANONE:        /* No data encoding specified */
  3637.     default:            /* Unknown data encoding specified */
  3638.       goto wrong;
  3639.     }
  3640.  
  3641.   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
  3642.      the tdata pointer in the bfd. */
  3643.  
  3644.   elf_tdata (abfd) =
  3645.     (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
  3646.   if (elf_tdata (abfd) == NULL)
  3647.     {
  3648.       bfd_set_error (bfd_error_no_memory);
  3649.       return NULL;
  3650.     }
  3651.  
  3652.   /* FIXME, `wrong' returns from this point onward, leak memory.  */
  3653.  
  3654.   /* Now that we know the byte order, swap in the rest of the header */
  3655.   i_ehdrp = elf_elfheader (abfd);
  3656.   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
  3657. #if DEBUG & 1
  3658.   elf_debug_file (i_ehdrp);
  3659. #endif
  3660.  
  3661.   ebd = get_elf_backend_data (abfd);
  3662.  
  3663.   /* Check that the ELF e_machine field matches what this particular
  3664.      BFD format expects.  */
  3665.   if (ebd->elf_machine_code != i_ehdrp->e_machine)
  3666.     {
  3667.       const bfd_target * const *target_ptr;
  3668.  
  3669.       if (ebd->elf_machine_code != EM_NONE)
  3670.     goto wrong;
  3671.  
  3672.       /* This is the generic ELF target.  Let it match any ELF target
  3673.      for which we do not have a specific backend.  */
  3674.       for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
  3675.     {
  3676.       struct elf_backend_data *back;
  3677.  
  3678.       if ((*target_ptr)->flavour != bfd_target_elf_flavour)
  3679.         continue;
  3680.       back = (struct elf_backend_data *) (*target_ptr)->backend_data;
  3681.       if (back->elf_machine_code == i_ehdrp->e_machine)
  3682.         {
  3683.           /* target_ptr is an ELF backend which matches this
  3684.          object file, so reject the generic ELF target.  */
  3685.           goto wrong;
  3686.         }
  3687.     }
  3688.     }
  3689.  
  3690.   /* If there is no program header, or the type is not a core file, then
  3691.      we are hosed. */
  3692.   if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
  3693.     goto wrong;
  3694.  
  3695.   /* Allocate space for a copy of the program header table in
  3696.      internal form, seek to the program header table in the file,
  3697.      read it in, and convert it to internal form.  As a simple sanity
  3698.      check, verify that the what BFD thinks is the size of each program
  3699.      header table entry actually matches the size recorded in the file. */
  3700.  
  3701.   if (i_ehdrp->e_phentsize != sizeof (x_phdr))
  3702.     goto wrong;
  3703.   i_phdrp = (Elf_Internal_Phdr *)
  3704.     bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
  3705.   if (!i_phdrp)
  3706.     {
  3707.       bfd_set_error (bfd_error_no_memory);
  3708.       return NULL;
  3709.     }
  3710.   if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
  3711.     return NULL;
  3712.   for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
  3713.     {
  3714.       if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
  3715.       != sizeof (x_phdr))
  3716.     return NULL;
  3717.       elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
  3718.     }
  3719.  
  3720.   /* Once all of the program headers have been read and converted, we
  3721.      can start processing them. */
  3722.  
  3723.   for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
  3724.     {
  3725.       bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
  3726.       if ((i_phdrp + phindex)->p_type == PT_NOTE)
  3727.     {
  3728.       if (! elf_corefile_note (abfd, i_phdrp + phindex))
  3729.         return NULL;
  3730.     }
  3731.     }
  3732.  
  3733.   /* Remember the entry point specified in the ELF file header. */
  3734.  
  3735.   bfd_get_start_address (abfd) = i_ehdrp->e_entry;
  3736.  
  3737.   return abfd->xvec;
  3738. }
  3739.  
  3740. /* ELF linker code.  */
  3741.  
  3742. static boolean elf_link_add_object_symbols
  3743.   PARAMS ((bfd *, struct bfd_link_info *));
  3744. static boolean elf_link_add_archive_symbols
  3745.   PARAMS ((bfd *, struct bfd_link_info *));
  3746. static Elf_Internal_Rela *elf_link_read_relocs
  3747.   PARAMS ((bfd *, asection *, PTR, Elf_Internal_Rela *, boolean));
  3748. static boolean elf_adjust_dynamic_symbol
  3749.   PARAMS ((struct elf_link_hash_entry *, PTR));
  3750.  
  3751. /* Given an ELF BFD, add symbols to the global hash table as
  3752.    appropriate.  */
  3753.  
  3754. boolean
  3755. elf_bfd_link_add_symbols (abfd, info)
  3756.      bfd *abfd;
  3757.      struct bfd_link_info *info;
  3758. {
  3759.   switch (bfd_get_format (abfd))
  3760.     {
  3761.     case bfd_object:
  3762.       return elf_link_add_object_symbols (abfd, info);
  3763.     case bfd_archive:
  3764.       return elf_link_add_archive_symbols (abfd, info);
  3765.     default:
  3766.       bfd_set_error (bfd_error_wrong_format);
  3767.       return false;
  3768.     }
  3769. }
  3770.  
  3771. /* Add symbols from an ELF archive file to the linker hash table.  We
  3772.    don't use _bfd_generic_link_add_archive_symbols because of a
  3773.    problem which arises on UnixWare.  The UnixWare libc.so is an
  3774.    archive which includes an entry libc.so.1 which defines a bunch of
  3775.    symbols.  The libc.so archive also includes a number of other
  3776.    object files, which also define symbols, some of which are the same
  3777.    as those defined in libc.so.1.  Correct linking requires that we
  3778.    consider each object file in turn, and include it if it defines any
  3779.    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
  3780.    this; it looks through the list of undefined symbols, and includes
  3781.    any object file which defines them.  When this algorithm is used on
  3782.    UnixWare, it winds up pulling in libc.so.1 early and defining a
  3783.    bunch of symbols.  This means that some of the other objects in the
  3784.    archive are not included in the link, which is incorrect since they
  3785.    precede libc.so.1 in the archive.
  3786.  
  3787.    Fortunately, ELF archive handling is simpler than that done by
  3788.    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
  3789.    oddities.  In ELF, if we find a symbol in the archive map, and the
  3790.    symbol is currently undefined, we know that we must pull in that
  3791.    object file.
  3792.  
  3793.    Unfortunately, we do have to make multiple passes over the symbol
  3794.    table until nothing further is resolved.  */
  3795.  
  3796. static boolean
  3797. elf_link_add_archive_symbols (abfd, info)
  3798.      bfd *abfd;
  3799.      struct bfd_link_info *info;
  3800. {
  3801.   symindex c;
  3802.   boolean *defined = NULL;
  3803.   boolean *included = NULL;
  3804.   carsym *symdefs;
  3805.   boolean loop;
  3806.  
  3807.   if (! bfd_has_map (abfd))
  3808.     {
  3809.       bfd_set_error (bfd_error_no_symbols);
  3810.       return false;
  3811.     }
  3812.  
  3813.   /* Keep track of all symbols we know to be already defined, and all
  3814.      files we know to be already included.  This is to speed up the
  3815.      second and subsequent passes.  */
  3816.   c = bfd_ardata (abfd)->symdef_count;
  3817.   if (c == 0)
  3818.     return true;
  3819.   defined = (boolean *) malloc (c * sizeof (boolean));
  3820.   included = (boolean *) malloc (c * sizeof (boolean));
  3821.   if (defined == (boolean *) NULL || included == (boolean *) NULL)
  3822.     {
  3823.       bfd_set_error (bfd_error_no_memory);
  3824.       goto error_return;
  3825.     }
  3826.   memset (defined, 0, c * sizeof (boolean));
  3827.   memset (included, 0, c * sizeof (boolean));
  3828.  
  3829.   symdefs = bfd_ardata (abfd)->symdefs;
  3830.  
  3831.   do
  3832.     {
  3833.       file_ptr last;
  3834.       symindex i;
  3835.       carsym *symdef;
  3836.       carsym *symdefend;
  3837.  
  3838.       loop = false;
  3839.       last = -1;
  3840.  
  3841.       symdef = symdefs;
  3842.       symdefend = symdef + c;
  3843.       for (i = 0; symdef < symdefend; symdef++, i++)
  3844.     {
  3845.       struct elf_link_hash_entry *h;
  3846.       bfd *element;
  3847.       struct bfd_link_hash_entry *undefs_tail;
  3848.       symindex mark;
  3849.  
  3850.       if (defined[i] || included[i])
  3851.         continue;
  3852.       if (symdef->file_offset == last)
  3853.         {
  3854.           included[i] = true;
  3855.           continue;
  3856.         }
  3857.  
  3858.       h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
  3859.                     false, false, false);
  3860.       if (h == (struct elf_link_hash_entry *) NULL)
  3861.         continue;
  3862.       if (h->root.type != bfd_link_hash_undefined)
  3863.         {
  3864.           defined[i] = true;
  3865.           continue;
  3866.         }
  3867.  
  3868.       /* We need to include this archive member.  */
  3869.  
  3870.       element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
  3871.       if (element == (bfd *) NULL)
  3872.         goto error_return;
  3873.  
  3874.       if (! bfd_check_format (element, bfd_object))
  3875.         goto error_return;
  3876.  
  3877.       /* Doublecheck that we have not included this object
  3878.          already--it should be impossible, but there may be
  3879.          something wrong with the archive.  */
  3880.       if (element->archive_pass != 0)
  3881.         {
  3882.           bfd_set_error (bfd_error_bad_value);
  3883.           goto error_return;
  3884.         }
  3885.       element->archive_pass = 1;
  3886.  
  3887.       undefs_tail = info->hash->undefs_tail;
  3888.  
  3889.       if (! (*info->callbacks->add_archive_element) (info, element,
  3890.                              symdef->name))
  3891.         goto error_return;
  3892.       if (! elf_link_add_object_symbols (element, info))
  3893.         goto error_return;
  3894.  
  3895.       /* If there are any new undefined symbols, we need to make
  3896.          another pass through the archive in order to see whether
  3897.          they can be defined.  FIXME: This isn't perfect, because
  3898.          common symbols wind up on undefs_tail and because an
  3899.          undefined symbol which is defined later on in this pass
  3900.          does not require another pass.  This isn't a bug, but it
  3901.          does make the code less efficient than it could be.  */
  3902.       if (undefs_tail != info->hash->undefs_tail)
  3903.         loop = true;
  3904.  
  3905.       /* Look backward to mark all symbols from this object file
  3906.          which we have already seen in this pass.  */
  3907.       mark = i;
  3908.       do
  3909.         {
  3910.           included[mark] = true;
  3911.           if (mark == 0)
  3912.         break;
  3913.           --mark;
  3914.         }
  3915.       while (symdefs[mark].file_offset == symdef->file_offset);
  3916.  
  3917.       /* We mark subsequent symbols from this object file as we go
  3918.          on through the loop.  */
  3919.       last = symdef->file_offset;
  3920.     }
  3921.     }
  3922.   while (loop);
  3923.  
  3924.   free (defined);
  3925.   free (included);
  3926.  
  3927.   return true;
  3928.  
  3929.  error_return:
  3930.   if (defined != (boolean *) NULL)
  3931.     free (defined);
  3932.   if (included != (boolean *) NULL)
  3933.     free (included);
  3934.   return false;
  3935. }
  3936.  
  3937. /* Record a new dynamic symbol.  We record the dynamic symbols as we
  3938.    read the input files, since we need to have a list of all of them
  3939.    before we can determine the final sizes of the output sections.
  3940.    Note that we may actually call this function even though we are not
  3941.    going to output any dynamic symbols; in some cases we know that a
  3942.    symbol should be in the dynamic symbol table, but only if there is
  3943.    one.  */
  3944.  
  3945. boolean
  3946. elf_link_record_dynamic_symbol (info, h)
  3947.      struct bfd_link_info *info;
  3948.      struct elf_link_hash_entry *h;
  3949. {
  3950.   if (h->dynindx == -1)
  3951.     {
  3952.       struct strtab *dynstr;
  3953.  
  3954.       h->dynindx = elf_hash_table (info)->dynsymcount;
  3955.       ++elf_hash_table (info)->dynsymcount;
  3956.  
  3957.       dynstr = elf_hash_table (info)->dynstr;
  3958.       if (dynstr == NULL)
  3959.     {
  3960.       /* Create a strtab to hold the dynamic symbol names.  */
  3961.       elf_hash_table (info)->dynstr = dynstr = bfd_new_strtab ();
  3962.       if (dynstr == NULL)
  3963.         return false;
  3964.     }
  3965.  
  3966.       h->dynstr_index = bfd_add_to_strtab (elf_hash_table (info)->dynobj,
  3967.                        dynstr, h->root.root.string);
  3968.  
  3969.       if (h->dynstr_index == (unsigned long) -1)
  3970.     return false;
  3971.     }
  3972.  
  3973.   return true;
  3974. }
  3975.  
  3976. /* Add symbols from an ELF object file to the linker hash table.  */
  3977.  
  3978. static boolean
  3979. elf_link_add_object_symbols (abfd, info)
  3980.      bfd *abfd;
  3981.      struct bfd_link_info *info;
  3982. {
  3983.   boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
  3984.                       const Elf_Internal_Sym *,
  3985.                       const char **, flagword *,
  3986.                       asection **, bfd_vma *));
  3987.   boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
  3988.                    asection *, const Elf_Internal_Rela *));
  3989.   boolean collect;
  3990.   Elf_Internal_Shdr *hdr;
  3991.   size_t symcount;
  3992.   size_t extsymcount;
  3993.   size_t extsymoff;
  3994.   Elf_External_Sym *buf = NULL;
  3995.   struct elf_link_hash_entry **sym_hash;
  3996.   boolean dynamic;
  3997.   Elf_External_Dyn *dynbuf = NULL;
  3998.   struct elf_link_hash_entry *weaks;
  3999.   Elf_External_Sym *esym;
  4000.   Elf_External_Sym *esymend;
  4001.  
  4002.   add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook;
  4003.   collect = get_elf_backend_data (abfd)->collect;
  4004.  
  4005.   /* A stripped shared library might only have a dynamic symbol table,
  4006.      not a regular symbol table.  In that case we can still go ahead
  4007.      and link using the dynamic symbol table.  */
  4008.   if (elf_onesymtab (abfd) == 0
  4009.       && elf_dynsymtab (abfd) != 0)
  4010.     {
  4011.       elf_onesymtab (abfd) = elf_dynsymtab (abfd);
  4012.       elf_tdata (abfd)->symtab_hdr = elf_tdata (abfd)->dynsymtab_hdr;
  4013.     }
  4014.  
  4015.   hdr = &elf_tdata (abfd)->symtab_hdr;
  4016.   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  4017.  
  4018.   /* The sh_info field of the symtab header tells us where the
  4019.      external symbols start.  We don't care about the local symbols at
  4020.      this point.  */
  4021.   if (elf_bad_symtab (abfd))
  4022.     {
  4023.       extsymcount = symcount;
  4024.       extsymoff = 0;
  4025.     }
  4026.   else
  4027.     {
  4028.       extsymcount = symcount - hdr->sh_info;
  4029.       extsymoff = hdr->sh_info;
  4030.     }
  4031.  
  4032.   buf = (Elf_External_Sym *) malloc (extsymcount * sizeof (Elf_External_Sym));
  4033.   if (buf == NULL && extsymcount != 0)
  4034.     {
  4035.       bfd_set_error (bfd_error_no_memory);
  4036.       goto error_return;
  4037.     }
  4038.  
  4039.   /* We store a pointer to the hash table entry for each external
  4040.      symbol.  */
  4041.   sym_hash = ((struct elf_link_hash_entry **)
  4042.           bfd_alloc (abfd,
  4043.              extsymcount * sizeof (struct elf_link_hash_entry *)));
  4044.   if (sym_hash == NULL)
  4045.     {
  4046.       bfd_set_error (bfd_error_no_memory);
  4047.       goto error_return;
  4048.     }
  4049.   elf_sym_hashes (abfd) = sym_hash;
  4050.  
  4051.   if (elf_elfheader (abfd)->e_type != ET_DYN)
  4052.     {
  4053.       dynamic = false;
  4054.  
  4055.       /* If we are creating a shared library, create all the dynamic
  4056.          sections immediately.  We need to attach them to something,
  4057.          so we attach them to this BFD, provided it is the right
  4058.          format.  FIXME: If there are no input BFD's of the same
  4059.          format as the output, we can't make a shared library.  */
  4060.       if (info->shared
  4061.       && ! elf_hash_table (info)->dynamic_sections_created
  4062.       && abfd->xvec == info->hash->creator)
  4063.     {
  4064.       if (! elf_link_create_dynamic_sections (abfd, info))
  4065.         goto error_return;
  4066.     }
  4067.     }
  4068.   else
  4069.     {
  4070.       asection *s;
  4071.       const char *name;
  4072.       unsigned long strindex;
  4073.  
  4074.       dynamic = true;
  4075.  
  4076.       /* You can't use -r against a dynamic object.  Also, there's no
  4077.      hope of using a dynamic object which does not exactly match
  4078.      the format of the output file.  */
  4079.       if (info->relocateable
  4080.       || info->hash->creator != abfd->xvec)
  4081.     {
  4082.       bfd_set_error (bfd_error_invalid_operation);
  4083.       goto error_return;
  4084.     }
  4085.  
  4086.       /* Find the name to use in a DT_NEEDED entry that refers to this
  4087.      object.  If the object has a DT_SONAME entry, we use it.
  4088.      Otherwise, if the generic linker stuck something in
  4089.      elf_dt_needed_name, we use that.  Otherwise, we just use the
  4090.      file name.  */
  4091.       name = bfd_get_filename (abfd);
  4092.       if (elf_dt_needed_name (abfd) != NULL)
  4093.     name = elf_dt_needed_name (abfd);
  4094.       s = bfd_get_section_by_name (abfd, ".dynamic");
  4095.       if (s != NULL)
  4096.     {
  4097.       Elf_External_Dyn *extdyn;
  4098.       Elf_External_Dyn *extdynend;
  4099.  
  4100.       dynbuf = (Elf_External_Dyn *) malloc (s->_raw_size);
  4101.       if (dynbuf == NULL)
  4102.         {
  4103.           bfd_set_error (bfd_error_no_memory);
  4104.           goto error_return;
  4105.         }
  4106.  
  4107.       if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
  4108.                       (file_ptr) 0, s->_raw_size))
  4109.         goto error_return;
  4110.  
  4111.       extdyn = dynbuf;
  4112.       extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
  4113.       for (; extdyn < extdynend; extdyn++)
  4114.         {
  4115.           Elf_Internal_Dyn dyn;
  4116.  
  4117.           elf_swap_dyn_in (abfd, extdyn, &dyn);
  4118.           if (dyn.d_tag == DT_SONAME)
  4119.         {
  4120.           int elfsec;
  4121.           unsigned long link;
  4122.  
  4123.           elfsec = elf_section_from_bfd_section (abfd, s);
  4124.           if (elfsec == -1)
  4125.             goto error_return;
  4126.           link = elf_elfsections (abfd)[elfsec]->sh_link;
  4127.           name = elf_string_from_elf_section (abfd, link,
  4128.                               dyn.d_un.d_val);
  4129.           if (name == NULL)
  4130.             goto error_return;
  4131.  
  4132.           break;
  4133.         }
  4134.         }
  4135.  
  4136.       free (dynbuf);
  4137.       dynbuf = NULL;
  4138.     }
  4139.  
  4140.       /* We do not want to include any of the sections in a dynamic
  4141.      object in the output file.  We hack by simply clobbering the
  4142.      list of sections in the BFD.  This could be handled more
  4143.      cleanly by, say, a new section flag; the existing
  4144.      SEC_NEVER_LOAD flag is not the one we want, because that one
  4145.      still implies that the section takes up space in the output
  4146.      file.  */
  4147.       abfd->sections = NULL;
  4148.  
  4149.       /* If this is the first dynamic object found in the link, create
  4150.      the special sections required for dynamic linking.  */
  4151.       if (! elf_hash_table (info)->dynamic_sections_created)
  4152.     {
  4153.       if (! elf_link_create_dynamic_sections (abfd, info))
  4154.         goto error_return;
  4155.     }
  4156.  
  4157.       /* Add a DT_NEEDED entry for this dynamic object.  */
  4158.       strindex = bfd_add_to_strtab (abfd,
  4159.                     elf_hash_table (info)->dynstr,
  4160.                     name);
  4161.       if (strindex == (unsigned long) -1)
  4162.     goto error_return;
  4163.       if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
  4164.     goto error_return;
  4165.     }
  4166.  
  4167.   if (bfd_seek (abfd,
  4168.         hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
  4169.         SEEK_SET) != 0
  4170.       || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
  4171.       != extsymcount * sizeof (Elf_External_Sym)))
  4172.     goto error_return;
  4173.  
  4174.   weaks = NULL;
  4175.  
  4176.   esymend = buf + extsymcount;
  4177.   for (esym = buf; esym < esymend; esym++, sym_hash++)
  4178.     {
  4179.       Elf_Internal_Sym sym;
  4180.       int bind;
  4181.       bfd_vma value;
  4182.       asection *sec;
  4183.       flagword flags;
  4184.       const char *name;
  4185.       struct elf_link_hash_entry *h = NULL;
  4186.       boolean definition;
  4187.  
  4188.       elf_swap_symbol_in (abfd, esym, &sym);
  4189.  
  4190.       flags = BSF_NO_FLAGS;
  4191.       sec = NULL;
  4192.       value = sym.st_value;
  4193.       *sym_hash = NULL;
  4194.  
  4195.       bind = ELF_ST_BIND (sym.st_info);
  4196.       if (bind == STB_LOCAL)
  4197.     {
  4198.       /* This should be impossible, since ELF requires that all
  4199.          global symbols follow all local symbols, and that sh_info
  4200.          point to the first global symbol.  Unfortunatealy, Irix 5
  4201.          screws this up.  */
  4202.       continue;
  4203.     }
  4204.       else if (bind == STB_GLOBAL)
  4205.     flags = BSF_GLOBAL;
  4206.       else if (bind == STB_WEAK)
  4207.     flags = BSF_WEAK;
  4208.       else
  4209.     {
  4210.       /* Leave it up to the processor backend.  */
  4211.     }
  4212.  
  4213.       if (sym.st_shndx == SHN_UNDEF)
  4214.     sec = bfd_und_section_ptr;
  4215.       else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
  4216.     {
  4217.       sec = section_from_elf_index (abfd, sym.st_shndx);
  4218.       if (sec == NULL)
  4219.         goto error_return;
  4220.       value -= sec->vma;
  4221.     }
  4222.       else if (sym.st_shndx == SHN_ABS)
  4223.     sec = bfd_abs_section_ptr;
  4224.       else if (sym.st_shndx == SHN_COMMON)
  4225.     {
  4226.       sec = bfd_com_section_ptr;
  4227.       /* What ELF calls the size we call the value.  What ELF
  4228.          calls the value we call the alignment.  */
  4229.       value = sym.st_size;
  4230.     }
  4231.       else
  4232.     {
  4233.       /* Leave it up to the processor backend.  */
  4234.     }
  4235.  
  4236.       name = elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
  4237.       if (name == (const char *) NULL)
  4238.     goto error_return;
  4239.  
  4240.       if (add_symbol_hook)
  4241.     {
  4242.       if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
  4243.                     &value))
  4244.         goto error_return;
  4245.  
  4246.       /* The hook function sets the name to NULL if this symbol
  4247.          should be skipped for some reason.  */
  4248.       if (name == (const char *) NULL)
  4249.         continue;
  4250.     }
  4251.  
  4252.       /* Sanity check that all possibilities were handled.  */
  4253.       if (flags == BSF_NO_FLAGS || sec == (asection *) NULL)
  4254.     {
  4255.       bfd_set_error (bfd_error_bad_value);
  4256.       goto error_return;
  4257.     }
  4258.  
  4259.       if (bfd_is_und_section (sec)
  4260.       || bfd_is_com_section (sec))
  4261.     definition = false;
  4262.       else
  4263.     definition = true;
  4264.  
  4265.       if (info->hash->creator->flavour == bfd_target_elf_flavour)
  4266.     {
  4267.       /* We need to look up the symbol now in order to get some of
  4268.          the dynamic object handling right.  We pass the hash
  4269.          table entry in to _bfd_generic_link_add_one_symbol so
  4270.          that it does not have to look it up again.  */
  4271.       h = elf_link_hash_lookup (elf_hash_table (info), name,
  4272.                     true, false, false);
  4273.       if (h == NULL)
  4274.         goto error_return;
  4275.       *sym_hash = h;
  4276.  
  4277.       /* If we are looking at a dynamic object, and this is a
  4278.          definition, we need to see if it has already been defined
  4279.          by some other object.  If it has, we want to use the
  4280.          existing definition, and we do not want to report a
  4281.          multiple symbol definition error; we do this by
  4282.          clobbering sec to be bfd_und_section_ptr.  */
  4283.       if (dynamic && definition)
  4284.         {
  4285.           if (h->root.type == bfd_link_hash_defined)
  4286.         sec = bfd_und_section_ptr;
  4287.         }
  4288.  
  4289.       /* Similarly, if we are not looking at a dynamic object, and
  4290.          we have a definition, we want to override any definition
  4291.          we may have from a dynamic object.  Symbols from regular
  4292.          files always take precedence over symbols from dynamic
  4293.          objects, even if they are defined after the dynamic
  4294.          object in the link.  */
  4295.       if (! dynamic
  4296.           && definition
  4297.           && h->root.type == bfd_link_hash_defined
  4298.           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  4299.           && (bfd_get_flavour (h->root.u.def.section->owner)
  4300.           == bfd_target_elf_flavour)
  4301.           && (elf_elfheader (h->root.u.def.section->owner)->e_type
  4302.           == ET_DYN))
  4303.         {
  4304.           /* Change the hash table entry to undefined, and let
  4305.          _bfd_generic_link_add_one_symbol do the right thing
  4306.          with the new definition.  */
  4307.           h->root.type = bfd_link_hash_undefined;
  4308.           h->root.u.undef.abfd = h->root.u.def.section->owner;
  4309.           h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEFINED_WEAK;
  4310.         }
  4311.  
  4312.       /* If this is a weak definition which we are going to use,
  4313.          and the symbol is currently undefined, record that the
  4314.          definition is weak.  */
  4315.       if (definition
  4316.           && (flags & BSF_WEAK) != 0
  4317.           && ! bfd_is_und_section (sec)
  4318.           && (h->root.type == bfd_link_hash_new
  4319.           || h->root.type == bfd_link_hash_undefined
  4320.           || h->root.type == bfd_link_hash_weak))
  4321.         h->elf_link_hash_flags |= ELF_LINK_HASH_DEFINED_WEAK;
  4322.     }
  4323.  
  4324.       if (! (_bfd_generic_link_add_one_symbol
  4325.          (info, abfd, name, flags, sec, value, (const char *) NULL,
  4326.           false, collect, (struct bfd_link_hash_entry **) sym_hash)))
  4327.     goto error_return;
  4328.  
  4329.       if (dynamic
  4330.       && definition
  4331.       && (flags & BSF_WEAK) != 0
  4332.       && ELF_ST_TYPE (sym.st_info) != STT_FUNC
  4333.       && (*sym_hash)->weakdef == NULL)
  4334.     {
  4335.       /* Keep a list of all weak defined non function symbols from
  4336.          a dynamic object, using the weakdef field.  Later in this
  4337.          function we will set the weakdef field to the correct
  4338.          value.  We only put non-function symbols from dynamic
  4339.          objects on this list, because that happens to be the only
  4340.          time we need to know the normal symbol corresponding to a
  4341.          weak symbol, and the information is time consuming to
  4342.          figure out.  If the weakdef field is not already NULL,
  4343.          then this symbol was already defined by some previous
  4344.          dynamic object, and we will be using that previous
  4345.          definition anyhow.  */
  4346.  
  4347.       (*sym_hash)->weakdef = weaks;
  4348.       weaks = *sym_hash;
  4349.     }
  4350.  
  4351.       /* Get the alignment of a common symbol.  */
  4352.       if (sym.st_shndx == SHN_COMMON
  4353.       && h->root.type == bfd_link_hash_common)
  4354.     h->root.u.c.alignment_power = bfd_log2 (sym.st_value);
  4355.  
  4356.       if (info->hash->creator->flavour == bfd_target_elf_flavour)
  4357.     {
  4358.       int old_flags;
  4359.       boolean dynsym;
  4360.       int new_flag;
  4361.  
  4362.       /* Remember the symbol size and type.  */
  4363.       if (sym.st_size != 0)
  4364.         {
  4365.           /* FIXME: We should probably somehow give a warning if
  4366.          the symbol size changes.  */
  4367.           h->size = sym.st_size;
  4368.         }
  4369.       if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE)
  4370.         {
  4371.           /* FIXME: We should probably somehow give a warning if
  4372.          the symbol type changes.  */
  4373.           h->type = ELF_ST_TYPE (sym.st_info);
  4374.         }
  4375.  
  4376.       /* Set a flag in the hash table entry indicating the type of
  4377.          reference or definition we just found.  Keep a count of
  4378.          the number of dynamic symbols we find.  A dynamic symbol
  4379.          is one which is referenced or defined by both a regular
  4380.          object and a shared object, or one which is referenced or
  4381.          defined by more than one shared object.  */
  4382.       old_flags = h->elf_link_hash_flags;
  4383.       dynsym = false;
  4384.       if (! dynamic)
  4385.         {
  4386.           if (! definition)
  4387.         new_flag = ELF_LINK_HASH_REF_REGULAR;
  4388.           else
  4389.         new_flag = ELF_LINK_HASH_DEF_REGULAR;
  4390.           if (info->shared
  4391.           || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
  4392.                    | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
  4393.         dynsym = true;
  4394.         }
  4395.       else
  4396.         {
  4397.           if (! definition)
  4398.         new_flag = ELF_LINK_HASH_REF_DYNAMIC;
  4399.           else
  4400.         new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
  4401.           if ((old_flags & new_flag) != 0
  4402.           || (old_flags & (ELF_LINK_HASH_DEF_REGULAR
  4403.                    | ELF_LINK_HASH_REF_REGULAR)) != 0)
  4404.         dynsym = true;
  4405.         }
  4406.  
  4407.       h->elf_link_hash_flags |= new_flag;
  4408.       if (dynsym && h->dynindx == -1)
  4409.         {
  4410.           if (! elf_link_record_dynamic_symbol (info, h))
  4411.         goto error_return;
  4412.         }
  4413.     }
  4414.     }
  4415.  
  4416.   /* Now set the weakdefs field correctly for all the weak defined
  4417.      symbols we found.  The only way to do this is to search all the
  4418.      symbols.  Since we only need the information for non functions in
  4419.      dynamic objects, that's the only time we actually put anything on
  4420.      the list WEAKS.  We need this information so that if a regular
  4421.      object refers to a symbol defined weakly in a dynamic object, the
  4422.      real symbol in the dynamic object is also put in the dynamic
  4423.      symbols; we also must arrange for both symbols to point to the
  4424.      same memory location.  We could handle the general case of symbol
  4425.      aliasing, but a general symbol alias can only be generated in
  4426.      assembler code, handling it correctly would be very time
  4427.      consuming, and other ELF linkers don't handle general aliasing
  4428.      either.  */
  4429.   while (weaks != NULL)
  4430.     {
  4431.       struct elf_link_hash_entry *hlook;
  4432.       asection *slook;
  4433.       bfd_vma vlook;
  4434.       struct elf_link_hash_entry **hpp;
  4435.       struct elf_link_hash_entry **hppend;
  4436.  
  4437.       hlook = weaks;
  4438.       weaks = hlook->weakdef;
  4439.       hlook->weakdef = NULL;
  4440.  
  4441.       BFD_ASSERT (hlook->root.type == bfd_link_hash_defined);
  4442.       slook = hlook->root.u.def.section;
  4443.       vlook = hlook->root.u.def.value;
  4444.  
  4445.       hpp = elf_sym_hashes (abfd);
  4446.       hppend = hpp + extsymcount;
  4447.       for (; hpp < hppend; hpp++)
  4448.     {
  4449.       struct elf_link_hash_entry *h;
  4450.  
  4451.       h = *hpp;
  4452.       if (h != hlook
  4453.           && h->root.type == bfd_link_hash_defined
  4454.           && h->root.u.def.section == slook
  4455.           && h->root.u.def.value == vlook)
  4456.         {
  4457.           hlook->weakdef = h;
  4458.  
  4459.           /* If the weak definition is in the list of dynamic
  4460.          symbols, make sure the real definition is put there
  4461.          as well.  */
  4462.           if (hlook->dynindx != -1
  4463.           && h->dynindx == -1)
  4464.         {
  4465.           if (! elf_link_record_dynamic_symbol (info, h))
  4466.             goto error_return;
  4467.         }
  4468.  
  4469.           break;
  4470.         }
  4471.     }
  4472.     }
  4473.  
  4474.   if (buf != NULL)
  4475.     {
  4476.       free (buf);
  4477.       buf = NULL;
  4478.     }
  4479.  
  4480.   /* If this object is the same format as the output object, and it is
  4481.      not a shared library, then let the backend look through the
  4482.      relocs.
  4483.  
  4484.      This is required to build global offset table entries and to
  4485.      arrange for dynamic relocs.  It is not required for the
  4486.      particular common case of linking non PIC code, even when linking
  4487.      against shared libraries, but unfortunately there is no way of
  4488.      knowing whether an object file has been compiled PIC or not.
  4489.      Looking through the relocs is not particularly time consuming.
  4490.      The problem is that we must either (1) keep the relocs in memory,
  4491.      which causes the linker to require additional runtime memory or
  4492.      (2) read the relocs twice from the input file, which wastes time.
  4493.      This would be a good case for using mmap.
  4494.  
  4495.      I have no idea how to handle linking PIC code into a file of a
  4496.      different format.  It probably can't be done.  */
  4497.   check_relocs = get_elf_backend_data (abfd)->check_relocs;
  4498.   if (! dynamic
  4499.       && abfd->xvec == info->hash->creator
  4500.       && check_relocs != NULL)
  4501.     {
  4502.       asection *o;
  4503.  
  4504.       for (o = abfd->sections; o != NULL; o = o->next)
  4505.     {
  4506.       Elf_Internal_Rela *internal_relocs;
  4507.       boolean ok;
  4508.  
  4509.       if ((o->flags & SEC_RELOC) == 0
  4510.           || o->reloc_count == 0)
  4511.         continue;
  4512.  
  4513.       /* I believe we can ignore the relocs for any section which
  4514.              does not form part of the final process image, such as a
  4515.              debugging section.  */
  4516.       if ((o->flags & SEC_ALLOC) == 0)
  4517.         continue;
  4518.  
  4519.       internal_relocs = elf_link_read_relocs (abfd, o, (PTR) NULL,
  4520.                           (Elf_Internal_Rela *) NULL,
  4521.                           info->keep_memory);
  4522.       if (internal_relocs == NULL)
  4523.         goto error_return;
  4524.  
  4525.       ok = (*check_relocs) (abfd, info, o, internal_relocs);
  4526.  
  4527.       if (! info->keep_memory)
  4528.         free (internal_relocs);
  4529.  
  4530.       if (! ok)
  4531.         goto error_return;
  4532.     }
  4533.     }
  4534.  
  4535.   return true;
  4536.  
  4537.  error_return:
  4538.   if (buf != NULL)
  4539.     free (buf);
  4540.   if (dynbuf != NULL)
  4541.     free (dynbuf);
  4542.   return false;
  4543. }
  4544.  
  4545. /* Create some sections which will be filled in with dynamic linking
  4546.    information.  ABFD is an input file which requires dynamic sections
  4547.    to be created.  The dynamic sections take up virtual memory space
  4548.    when the final executable is run, so we need to create them before
  4549.    addresses are assigned to the output sections.  We work out the
  4550.    actual contents and size of these sections later.  */
  4551.  
  4552. boolean
  4553. elf_link_create_dynamic_sections (abfd, info)
  4554.      bfd *abfd;
  4555.      struct bfd_link_info *info;
  4556. {
  4557.   flagword flags;
  4558.   register asection *s;
  4559.   struct elf_link_hash_entry *h;
  4560.   struct elf_backend_data *bed;
  4561.  
  4562.   if (elf_hash_table (info)->dynamic_sections_created)
  4563.     return true;
  4564.  
  4565.   /* Make sure that all dynamic sections use the same input BFD.  */
  4566.   if (elf_hash_table (info)->dynobj == NULL)
  4567.     elf_hash_table (info)->dynobj = abfd;
  4568.   else
  4569.     abfd = elf_hash_table (info)->dynobj;
  4570.  
  4571.   /* Note that we set the SEC_IN_MEMORY flag for all of these
  4572.      sections.  */
  4573.   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  4574.  
  4575.   /* A dynamically linked executable has a .interp section, but a
  4576.      shared library does not.  */
  4577.   if (! info->shared)
  4578.     {
  4579.       s = bfd_make_section (abfd, ".interp");
  4580.       if (s == NULL
  4581.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
  4582.     return false;
  4583.     }
  4584.  
  4585.   s = bfd_make_section (abfd, ".dynsym");
  4586.   if (s == NULL
  4587.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  4588.       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
  4589.     return false;
  4590.  
  4591.   s = bfd_make_section (abfd, ".dynstr");
  4592.   if (s == NULL
  4593.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
  4594.     return false;
  4595.  
  4596.   /* Create a strtab to hold the dynamic symbol names.  */
  4597.   if (elf_hash_table (info)->dynstr == NULL)
  4598.     {
  4599.       elf_hash_table (info)->dynstr = bfd_new_strtab (abfd);
  4600.       if (elf_hash_table (info)->dynstr == NULL)
  4601.     return false;
  4602.     }
  4603.  
  4604.   s = bfd_make_section (abfd, ".dynamic");
  4605.   if (s == NULL
  4606.       || ! bfd_set_section_flags (abfd, s, flags)
  4607.       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
  4608.     return false;
  4609.  
  4610.   /* The special symbol _DYNAMIC is always set to the start of the
  4611.      .dynamic section.  This call occurs before we have processed the
  4612.      symbols for any dynamic object, so we don't have to worry about
  4613.      overriding a dynamic definition.  We could set _DYNAMIC in a
  4614.      linker script, but we only want to define it if we are, in fact,
  4615.      creating a .dynamic section.  We don't want to define it if there
  4616.      is no .dynamic section, since on some ELF platforms the start up
  4617.      code examines it to decide how to initialize the process.  */
  4618.   h = NULL;
  4619.   if (! (_bfd_generic_link_add_one_symbol
  4620.      (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
  4621.       (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
  4622.       (struct bfd_link_hash_entry **) &h)))
  4623.     return false;
  4624.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  4625.   h->type = STT_OBJECT;
  4626.  
  4627.   if (info->shared
  4628.       && ! elf_link_record_dynamic_symbol (info, h))
  4629.     return false;
  4630.  
  4631.   s = bfd_make_section (abfd, ".hash");
  4632.   if (s == NULL
  4633.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  4634.       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
  4635.     return false;
  4636.  
  4637.   /* Let the backend create the rest of the sections.  This lets the
  4638.      backend set the right flags.  The backend will normally create
  4639.      the .got and .plt sections.  */
  4640.   bed = get_elf_backend_data (abfd);
  4641.   if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
  4642.     return false;
  4643.  
  4644.   elf_hash_table (info)->dynamic_sections_created = true;
  4645.  
  4646.   return true;
  4647. }
  4648.  
  4649. /* Add an entry to the .dynamic table.  */
  4650.  
  4651. boolean
  4652. elf_add_dynamic_entry (info, tag, val)
  4653.      struct bfd_link_info *info;
  4654.      bfd_vma tag;
  4655.      bfd_vma val;
  4656. {
  4657.   Elf_Internal_Dyn dyn;
  4658.   bfd *dynobj;
  4659.   asection *s;
  4660.   size_t newsize;
  4661.   bfd_byte *newcontents;
  4662.  
  4663.   dynobj = elf_hash_table (info)->dynobj;
  4664.  
  4665.   s = bfd_get_section_by_name (dynobj, ".dynamic");
  4666.   BFD_ASSERT (s != NULL);
  4667.  
  4668.   newsize = s->_raw_size + sizeof (Elf_External_Dyn);
  4669.   if (s->contents == NULL)
  4670.     newcontents = (bfd_byte *) malloc (newsize);
  4671.   else
  4672.     newcontents = (bfd_byte *) realloc (s->contents, newsize);
  4673.   if (newcontents == NULL)
  4674.     {
  4675.       bfd_set_error (bfd_error_no_memory);
  4676.       return false;
  4677.     }
  4678.  
  4679.   dyn.d_tag = tag;
  4680.   dyn.d_un.d_val = val;
  4681.   elf_swap_dyn_out (dynobj, &dyn,
  4682.             (Elf_External_Dyn *) (newcontents + s->_raw_size));
  4683.  
  4684.   s->_raw_size = newsize;
  4685.   s->contents = newcontents;
  4686.  
  4687.   return true;
  4688. }
  4689.  
  4690. /* Read and swap the relocs for a section.  They may have been cached.
  4691.    If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
  4692.    they are used as buffers to read into.  They are known to be large
  4693.    enough.  If the INTERNAL_RELOCS relocs argument is NULL, the return
  4694.    value is allocated using either malloc or bfd_alloc, according to
  4695.    the KEEP_MEMORY argument.  */
  4696.  
  4697. static Elf_Internal_Rela *
  4698. elf_link_read_relocs (abfd, o, external_relocs, internal_relocs, keep_memory)
  4699.      bfd *abfd;
  4700.      asection *o;
  4701.      PTR external_relocs;
  4702.      Elf_Internal_Rela *internal_relocs;
  4703.      boolean keep_memory;
  4704. {
  4705.   Elf_Internal_Shdr *rel_hdr;
  4706.   PTR alloc1 = NULL;
  4707.   Elf_Internal_Rela *alloc2 = NULL;
  4708.  
  4709.   if (elf_section_data (o)->relocs != NULL)
  4710.     return elf_section_data (o)->relocs;
  4711.  
  4712.   if (o->reloc_count == 0)
  4713.     return NULL;
  4714.  
  4715.   rel_hdr = &elf_section_data (o)->rel_hdr;
  4716.  
  4717.   if (internal_relocs == NULL)
  4718.     {
  4719.       size_t size;
  4720.  
  4721.       size = o->reloc_count * sizeof (Elf_Internal_Rela);
  4722.       if (keep_memory)
  4723.     internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
  4724.       else
  4725.     internal_relocs = alloc2 = (Elf_Internal_Rela *) malloc (size);
  4726.       if (internal_relocs == NULL)
  4727.     {
  4728.       bfd_set_error (bfd_error_no_memory);
  4729.       goto error_return;
  4730.     }
  4731.     }
  4732.  
  4733.   if (external_relocs == NULL)
  4734.     {
  4735.       alloc1 = (PTR) malloc (rel_hdr->sh_size);
  4736.       if (alloc1 == NULL)
  4737.     {
  4738.       bfd_set_error (bfd_error_no_memory);
  4739.       goto error_return;
  4740.     }
  4741.       external_relocs = alloc1;
  4742.     }
  4743.  
  4744.   if ((bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
  4745.       || (bfd_read (external_relocs, 1, rel_hdr->sh_size, abfd)
  4746.       != rel_hdr->sh_size))
  4747.     goto error_return;
  4748.  
  4749.   /* Swap in the relocs.  For convenience, we always produce an
  4750.      Elf_Internal_Rela array; if the relocs are Rel, we set the addend
  4751.      to 0.  */
  4752.   if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
  4753.     {
  4754.       Elf_External_Rel *erel;
  4755.       Elf_External_Rel *erelend;
  4756.       Elf_Internal_Rela *irela;
  4757.  
  4758.       erel = (Elf_External_Rel *) external_relocs;
  4759.       erelend = erel + o->reloc_count;
  4760.       irela = internal_relocs;
  4761.       for (; erel < erelend; erel++, irela++)
  4762.     {
  4763.       Elf_Internal_Rel irel;
  4764.  
  4765.       elf_swap_reloc_in (abfd, erel, &irel);
  4766.       irela->r_offset = irel.r_offset;
  4767.       irela->r_info = irel.r_info;
  4768.       irela->r_addend = 0;
  4769.     }
  4770.     }
  4771.   else
  4772.     {
  4773.       Elf_External_Rela *erela;
  4774.       Elf_External_Rela *erelaend;
  4775.       Elf_Internal_Rela *irela;
  4776.  
  4777.       BFD_ASSERT (rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
  4778.  
  4779.       erela = (Elf_External_Rela *) external_relocs;
  4780.       erelaend = erela + o->reloc_count;
  4781.       irela = internal_relocs;
  4782.       for (; erela < erelaend; erela++, irela++)
  4783.     elf_swap_reloca_in (abfd, erela, irela);
  4784.     }
  4785.  
  4786.   /* Cache the results for next time, if we can.  */
  4787.   if (keep_memory)
  4788.     elf_section_data (o)->relocs = internal_relocs;
  4789.          
  4790.   if (alloc1 != NULL)
  4791.     free (alloc1);
  4792.  
  4793.   /* Don't free alloc2, since if it was allocated we are passing it
  4794.      back (under the name of internal_relocs).  */
  4795.  
  4796.   return internal_relocs;
  4797.  
  4798.  error_return:
  4799.   if (alloc1 != NULL)
  4800.     free (alloc1);
  4801.   if (alloc2 != NULL)
  4802.     free (alloc2);
  4803.   return NULL;
  4804. }
  4805.  
  4806. /* Record an assignment to a symbol made by a linker script.  We need
  4807.    this in case some dynamic object refers to this symbol.  */
  4808.  
  4809. /*ARGSUSED*/
  4810. boolean
  4811. NAME(bfd_elf,record_link_assignment) (output_bfd, info, name)
  4812.      bfd *output_bfd;
  4813.      struct bfd_link_info *info;
  4814.      const char *name;
  4815. {
  4816.   struct elf_link_hash_entry *h;
  4817.  
  4818.   h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
  4819.   if (h == NULL)
  4820.     return false;
  4821.  
  4822.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  4823.   h->type = STT_OBJECT;
  4824.  
  4825.   if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
  4826.                   | ELF_LINK_HASH_REF_DYNAMIC)) != 0
  4827.        || info->shared)
  4828.       && h->dynindx == -1)
  4829.     {
  4830.       if (! elf_link_record_dynamic_symbol (info, h))
  4831.     return false;
  4832.  
  4833.       /* If this is a weak defined symbol, and we know a corresponding
  4834.      real symbol from the same dynamic object, make sure the real
  4835.      symbol is also made into a dynamic symbol.  */
  4836.       if (h->weakdef != NULL
  4837.       && h->weakdef->dynindx == -1)
  4838.     {
  4839.       if (! elf_link_record_dynamic_symbol (info, h->weakdef))
  4840.         return false;
  4841.     }
  4842.     }
  4843.  
  4844.   return true;
  4845. }
  4846.  
  4847. /* Array used to determine the number of hash table buckets to use
  4848.    based on the number of symbols there are.  If there are fewer than
  4849.    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
  4850.    fewer than 37 we use 17 buckets, and so forth.  We never use more
  4851.    than 521 buckets.  */
  4852.  
  4853. static const size_t elf_buckets[] =
  4854. {
  4855.   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 0
  4856. };
  4857.  
  4858. /* Set up the sizes and contents of the ELF dynamic sections.  This is
  4859.    called by the ELF linker emulation before_allocation routine.  We
  4860.    must set the sizes of the sections before the linker sets the
  4861.    addresses of the various sections.  */
  4862.  
  4863. boolean
  4864. NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath, info,
  4865.                      sinterpptr)
  4866.      bfd *output_bfd;
  4867.      const char *soname;
  4868.      const char *rpath;
  4869.      struct bfd_link_info *info;
  4870.      asection **sinterpptr;
  4871. {
  4872.   bfd *dynobj;
  4873.   asection *s;
  4874.   Elf_Internal_Sym isym;
  4875.   size_t i;
  4876.   size_t bucketcount;
  4877.   struct elf_backend_data *bed;
  4878.  
  4879.   *sinterpptr = NULL;
  4880.  
  4881.   dynobj = elf_hash_table (info)->dynobj;
  4882.  
  4883.   /* If there were no dynamic objects in the link, there is nothing to
  4884.      do here.  */
  4885.   if (dynobj == NULL)
  4886.     return true;
  4887.  
  4888.   if (elf_hash_table (info)->dynamic_sections_created)
  4889.     {
  4890.       size_t dynsymcount;
  4891.  
  4892.       dynsymcount = elf_hash_table (info)->dynsymcount;
  4893.  
  4894.       *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
  4895.       BFD_ASSERT (*sinterpptr != NULL || info->shared);
  4896.  
  4897.       /* Set the size of the .dynsym and .hash sections.  We counted
  4898.      the number of dynamic symbols in elf_link_add_object_symbols.
  4899.      We will build the contents of .dynsym and .hash when we build
  4900.      the final symbol table, because until then we do not know the
  4901.      correct value to give the symbols.  We built the .dynstr
  4902.      section as we went along in elf_link_add_object_symbols.  */
  4903.       s = bfd_get_section_by_name (dynobj, ".dynsym");
  4904.       BFD_ASSERT (s != NULL);
  4905.       s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
  4906.       s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
  4907.       if (s->contents == NULL && s->_raw_size != 0)
  4908.     {
  4909.       bfd_set_error (bfd_error_no_memory);
  4910.       return false;
  4911.     }
  4912.  
  4913.       /* The first entry in .dynsym is a dummy symbol.  */
  4914.       isym.st_value = 0;
  4915.       isym.st_size = 0;
  4916.       isym.st_name = 0;
  4917.       isym.st_info = 0;
  4918.       isym.st_other = 0;
  4919.       isym.st_shndx = 0;
  4920.       elf_swap_symbol_out (output_bfd, &isym,
  4921.                (Elf_External_Sym *) s->contents);
  4922.  
  4923.       for (i = 0; elf_buckets[i] != 0; i++)
  4924.     {
  4925.       bucketcount = elf_buckets[i];
  4926.       if (dynsymcount < elf_buckets[i + 1])
  4927.         break;
  4928.     }
  4929.  
  4930.       s = bfd_get_section_by_name (dynobj, ".hash");
  4931.       BFD_ASSERT (s != NULL);
  4932.       s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8);
  4933.       s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
  4934.       if (s->contents == NULL)
  4935.     {
  4936.       bfd_set_error (bfd_error_no_memory);
  4937.       return false;
  4938.     }
  4939.       memset (s->contents, 0, s->_raw_size);
  4940.  
  4941.       put_word (output_bfd, bucketcount, s->contents);
  4942.       put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8));
  4943.  
  4944.       elf_hash_table (info)->bucketcount = bucketcount;
  4945.  
  4946.       if (soname != NULL)
  4947.     {
  4948.       unsigned long indx;
  4949.  
  4950.       indx = bfd_add_to_strtab (dynobj, elf_hash_table (info)->dynstr,
  4951.                     soname);
  4952.       if (indx == (unsigned long) -1
  4953.           || ! elf_add_dynamic_entry (info, DT_SONAME, indx))
  4954.         return false;
  4955.     }      
  4956.  
  4957.       if (rpath != NULL)
  4958.     {
  4959.       unsigned long indx;
  4960.  
  4961.       indx = bfd_add_to_strtab (dynobj, elf_hash_table (info)->dynstr,
  4962.                     rpath);
  4963.       if (indx == (unsigned long) -1
  4964.           || ! elf_add_dynamic_entry (info, DT_RPATH, indx))
  4965.         return false;
  4966.     }
  4967.  
  4968.       s = bfd_get_section_by_name (dynobj, ".dynstr");
  4969.       BFD_ASSERT (s != NULL);
  4970.       s->_raw_size = elf_hash_table (info)->dynstr->length;
  4971.       s->contents = (unsigned char *) elf_hash_table (info)->dynstr->tab;
  4972.  
  4973.       /* Find all symbols which were defined in a dynamic object and
  4974.      make the backend pick a reasonable value for them.  */
  4975.       elf_link_hash_traverse (elf_hash_table (info),
  4976.                   elf_adjust_dynamic_symbol,
  4977.                   (PTR) info);
  4978.  
  4979.       /* Add some entries to the .dynamic section.  We fill in some of
  4980.      the values later, in elf_bfd_final_link, but we must add the
  4981.      entries now so that we know the final size of the .dynamic
  4982.      section.  */
  4983.       if (bfd_get_section_by_name (output_bfd, ".init") != NULL)
  4984.     {
  4985.       if (! elf_add_dynamic_entry (info, DT_INIT, 0))
  4986.         return false;
  4987.     }
  4988.       if (bfd_get_section_by_name (output_bfd, ".fini") != NULL)
  4989.     {
  4990.       if (! elf_add_dynamic_entry (info, DT_FINI, 0))
  4991.         return false;
  4992.     }
  4993.       if (! elf_add_dynamic_entry (info, DT_HASH, 0)
  4994.       || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
  4995.       || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
  4996.       || ! elf_add_dynamic_entry (info, DT_STRSZ,
  4997.                       elf_hash_table (info)->dynstr->length)
  4998.       || ! elf_add_dynamic_entry (info, DT_SYMENT,
  4999.                       sizeof (Elf_External_Sym)))
  5000.     return false;
  5001.     }
  5002.  
  5003.   /* The backend must work out the sizes of all the other dynamic
  5004.      sections.  */
  5005.   bed = get_elf_backend_data (output_bfd);
  5006.   if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
  5007.     return false;
  5008.  
  5009.   if (elf_hash_table (info)->dynamic_sections_created)
  5010.     {
  5011.       if (! elf_add_dynamic_entry (info, DT_NULL, 0))
  5012.     return false;
  5013.     }
  5014.  
  5015.   return true;
  5016. }
  5017.  
  5018. /* Make the backend pick a good value for a dynamic symbol.  This is
  5019.    called via elf_link_hash_traverse, and also calls itself
  5020.    recursively.  */
  5021.  
  5022. static boolean
  5023. elf_adjust_dynamic_symbol (h, data)
  5024.      struct elf_link_hash_entry *h;
  5025.      PTR data;
  5026. {
  5027.   struct bfd_link_info *info = (struct bfd_link_info *) data;
  5028.   bfd *dynobj;
  5029.   struct elf_backend_data *bed;
  5030.  
  5031.   /* If this symbol does not require a PLT entry, and it is not
  5032.      defined by a dynamic object, or is not referenced by a regular
  5033.      object, ignore it.  FIXME: Do we need to worry about symbols
  5034.      which are defined by one dynamic object and referenced by another
  5035.      one?  */
  5036.   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
  5037.       && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
  5038.       || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
  5039.       || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0))
  5040.     return true;
  5041.  
  5042.   /* If we've already adjusted this symbol, don't do it again.  This
  5043.      can happen via a recursive call.  */
  5044.   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
  5045.     return true;
  5046.  
  5047.   /* Don't look at this symbol again.  Note that we must set this
  5048.      after checking the above conditions, because we may look at a
  5049.      symbol once, decide not to do anything, and then get called
  5050.      recursively later after REF_REGULAR is set below.  */
  5051.   h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
  5052.  
  5053.   /* If this is a weak definition, and we know a real definition, and
  5054.      the real symbol is not itself defined by a regular object file,
  5055.      then get a good value for the real definition.  We handle the
  5056.      real symbol first, for the convenience of the backend routine.
  5057.  
  5058.      Note that there is a confusing case here.  If the real definition
  5059.      is defined by a regular object file, we don't get the real symbol
  5060.      from the dynamic object, but we do get the weak symbol.  If the
  5061.      processor backend uses a COPY reloc, then if some routine in the
  5062.      dynamic object changes the real symbol, we will not see that
  5063.      change in the corresponding weak symbol.  This is the way other
  5064.      ELF linkers work as well, and seems to be a result of the shared
  5065.      library model.
  5066.  
  5067.      I will clarify this issue.  Most SVR4 shared libraries define the
  5068.      variable _timezone and define timezone as a weak synonym.  The
  5069.      tzset call changes _timezone.  If you write
  5070.        extern int timezone;
  5071.        int _timezone = 5;
  5072.        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
  5073.      you might expect that, since timezone is a synonym for _timezone,
  5074.      the same number will print both times.  However, if the processor
  5075.      backend uses a COPY reloc, then actually timezone will be copied
  5076.      into your process image, and, since you define _timezone
  5077.      yourself, _timezone will not.  Thus timezone and _timezone will
  5078.      wind up at different memory locations.  The tzset call will set
  5079.      _timezone, leaving timezone unchanged.  */
  5080.  
  5081.   if (h->weakdef != NULL)
  5082.     {
  5083.       struct elf_link_hash_entry *weakdef;
  5084.  
  5085.       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
  5086.       weakdef = h->weakdef;
  5087.       BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined);
  5088.       BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
  5089.       if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
  5090.     {
  5091.       /* This symbol is defined by a regular object file, so we
  5092.          will not do anything special.  Clear weakdef for the
  5093.          convenience of the processor backend.  */
  5094.       h->weakdef = NULL;
  5095.     }
  5096.       else
  5097.     {
  5098.       /* There is an implicit reference by a regular object file
  5099.          via the weak symbol.  */
  5100.       weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
  5101.       if (! elf_adjust_dynamic_symbol (weakdef, (PTR) info))
  5102.         return false;
  5103.     }
  5104.     }
  5105.  
  5106.   dynobj = elf_hash_table (info)->dynobj;
  5107.   bed = get_elf_backend_data (dynobj);
  5108.   if (! (*bed->elf_backend_adjust_dynamic_symbol) (info, h))
  5109.     {
  5110.       /* FIXME: No way to return error.  */
  5111.       abort ();
  5112.     }
  5113.  
  5114.   return true;
  5115. }
  5116.  
  5117. /* Final phase of ELF linker.  */
  5118.  
  5119. /* A structure we use to avoid passing large numbers of arguments.  */
  5120.  
  5121. struct elf_final_link_info
  5122. {
  5123.   /* General link information.  */
  5124.   struct bfd_link_info *info;
  5125.   /* Output BFD.  */
  5126.   bfd *output_bfd;
  5127.   /* Symbol string table.  */
  5128.   struct strtab *symstrtab;
  5129.   /* .dynsym section.  */
  5130.   asection *dynsym_sec;
  5131.   /* .hash section.  */
  5132.   asection *hash_sec;
  5133.   /* Buffer large enough to hold contents of any section.  */
  5134.   bfd_byte *contents;
  5135.   /* Buffer large enough to hold external relocs of any section.  */
  5136.   PTR external_relocs;
  5137.   /* Buffer large enough to hold internal relocs of any section.  */
  5138.   Elf_Internal_Rela *internal_relocs;
  5139.   /* Buffer large enough to hold external local symbols of any input
  5140.      BFD.  */
  5141.   Elf_External_Sym *external_syms;
  5142.   /* Buffer large enough to hold internal local symbols of any input
  5143.      BFD.  */
  5144.   Elf_Internal_Sym *internal_syms;
  5145.   /* Array large enough to hold a symbol index for each local symbol
  5146.      of any input BFD.  */
  5147.   long *indices;
  5148.   /* Array large enough to hold a section pointer for each local
  5149.      symbol of any input BFD.  */
  5150.   asection **sections;
  5151.   /* Buffer to hold swapped out symbols.  */
  5152.   Elf_External_Sym *symbuf;
  5153.   /* Number of swapped out symbols in buffer.  */
  5154.   size_t symbuf_count;
  5155.   /* Number of symbols which fit in symbuf.  */
  5156.   size_t symbuf_size;
  5157. };
  5158.  
  5159. static boolean elf_link_output_sym
  5160.   PARAMS ((struct elf_final_link_info *, const char *,
  5161.        Elf_Internal_Sym *, asection *));
  5162. static boolean elf_link_flush_output_syms
  5163.   PARAMS ((struct elf_final_link_info *));
  5164. static boolean elf_link_output_extsym
  5165.   PARAMS ((struct elf_link_hash_entry *, PTR));
  5166. static boolean elf_link_input_bfd
  5167.   PARAMS ((struct elf_final_link_info *, bfd *));
  5168. static boolean elf_reloc_link_order
  5169.   PARAMS ((bfd *, struct bfd_link_info *, asection *,
  5170.        struct bfd_link_order *));
  5171.  
  5172. /* Do the final step of an ELF link.  */
  5173.  
  5174. boolean
  5175. elf_bfd_final_link (abfd, info)
  5176.      bfd *abfd;
  5177.      struct bfd_link_info *info;
  5178. {
  5179.   boolean dynamic;
  5180.   bfd *dynobj;
  5181.   struct elf_final_link_info finfo;
  5182.   register asection *o;
  5183.   register struct bfd_link_order *p;
  5184.   register bfd *sub;
  5185.   size_t max_contents_size;
  5186.   size_t max_external_reloc_size;
  5187.   size_t max_internal_reloc_count;
  5188.   size_t max_sym_count;
  5189.   file_ptr off;
  5190.   Elf_Internal_Sym elfsym;
  5191.   unsigned int i;
  5192.   Elf_Internal_Shdr *symtab_hdr;
  5193.   Elf_Internal_Shdr *symstrtab_hdr;
  5194.   struct elf_backend_data *bed = get_elf_backend_data (abfd);
  5195.  
  5196.   if (info->shared)
  5197.     abfd->flags |= DYNAMIC;
  5198.  
  5199.   dynamic = elf_hash_table (info)->dynamic_sections_created;
  5200.   dynobj = elf_hash_table (info)->dynobj;
  5201.  
  5202.   finfo.info = info;
  5203.   finfo.output_bfd = abfd;
  5204.   finfo.symstrtab = bfd_new_strtab (abfd);
  5205.   if (finfo.symstrtab == NULL)
  5206.     return false;
  5207.   if (! dynamic)
  5208.     {
  5209.       finfo.dynsym_sec = NULL;
  5210.       finfo.hash_sec = NULL;
  5211.     }
  5212.   else
  5213.     {
  5214.       finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
  5215.       finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
  5216.       if (finfo.dynsym_sec == NULL
  5217.       || finfo.hash_sec == NULL)
  5218.     abort ();
  5219.     }
  5220.   finfo.contents = NULL;
  5221.   finfo.external_relocs = NULL;
  5222.   finfo.internal_relocs = NULL;
  5223.   finfo.external_syms = NULL;
  5224.   finfo.internal_syms = NULL;
  5225.   finfo.indices = NULL;
  5226.   finfo.sections = NULL;
  5227.   finfo.symbuf = NULL;
  5228.   finfo.symbuf_count = 0;
  5229.  
  5230.   /* Count up the number of relocations we will output for each output
  5231.      section, so that we know the sizes of the reloc sections.  We
  5232.      also figure out some maximum sizes.  */
  5233.   max_contents_size = 0;
  5234.   max_external_reloc_size = 0;
  5235.   max_internal_reloc_count = 0;
  5236.   max_sym_count = 0;
  5237.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  5238.     {
  5239.       o->reloc_count = 0;
  5240.  
  5241.       for (p = o->link_order_head; p != NULL; p = p->next)
  5242.     {
  5243.       if (p->type == bfd_section_reloc_link_order
  5244.           || p->type == bfd_symbol_reloc_link_order)
  5245.         ++o->reloc_count;
  5246.       else if (p->type == bfd_indirect_link_order)
  5247.         {
  5248.           asection *sec;
  5249.  
  5250.           sec = p->u.indirect.section;
  5251.  
  5252.           if (info->relocateable)
  5253.         o->reloc_count += sec->reloc_count;
  5254.  
  5255.           if (sec->_raw_size > max_contents_size)
  5256.         max_contents_size = sec->_raw_size;
  5257.           if (sec->_cooked_size > max_contents_size)
  5258.         max_contents_size = sec->_cooked_size;
  5259.  
  5260.           /* We are interested in just local symbols, not all
  5261.          symbols.  */
  5262.           if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour)
  5263.         {
  5264.           size_t sym_count;
  5265.  
  5266.           if (elf_bad_symtab (sec->owner))
  5267.             sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
  5268.                  / sizeof (Elf_External_Sym));
  5269.           else
  5270.             sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
  5271.  
  5272.           if (sym_count > max_sym_count)
  5273.             max_sym_count = sym_count;
  5274.  
  5275.           if ((sec->flags & SEC_RELOC) != 0)
  5276.             {
  5277.               size_t ext_size;
  5278.  
  5279.               ext_size = elf_section_data (sec)->rel_hdr.sh_size;
  5280.               if (ext_size > max_external_reloc_size)
  5281.             max_external_reloc_size = ext_size;
  5282.               if (sec->reloc_count > max_internal_reloc_count)
  5283.             max_internal_reloc_count = sec->reloc_count;
  5284.             }
  5285.         }
  5286.         }
  5287.     }
  5288.  
  5289.       if (o->reloc_count > 0)
  5290.     o->flags |= SEC_RELOC;
  5291.       else
  5292.     {
  5293.       /* Explicitly clear the SEC_RELOC flag.  The linker tends to
  5294.          set it (this is probably a bug) and if it is set
  5295.          assign_section_numbers will create a reloc section.  */
  5296.       o->flags &=~ SEC_RELOC;
  5297.     }
  5298.  
  5299.       /* If the SEC_ALLOC flag is not set, force the section VMA to
  5300.      zero.  This is done in elf_fake_sections as well, but forcing
  5301.      the VMA to 0 here will ensure that relocs against these
  5302.      sections are handled correctly.  */
  5303.       if ((o->flags & SEC_ALLOC) == 0)
  5304.     o->vma = 0;
  5305.     }
  5306.  
  5307.   /* Figure out the file positions for everything but the symbol table
  5308.      and the relocs.  We set symcount to force assign_section_numbers
  5309.      to create a symbol table.  */
  5310.   abfd->symcount = info->strip == strip_all ? 0 : 1;
  5311.   BFD_ASSERT (! abfd->output_has_begun);
  5312.   if (! elf_compute_section_file_positions (abfd, info))
  5313.     goto error_return;
  5314.  
  5315.   /* That created the reloc sections.  Set their sizes, and assign
  5316.      them file positions, and allocate some buffers.  */
  5317.   for (o = abfd->sections; o != NULL; o = o->next)
  5318.     {
  5319.       if ((o->flags & SEC_RELOC) != 0)
  5320.     {
  5321.       Elf_Internal_Shdr *rel_hdr;
  5322.       register struct elf_link_hash_entry **p, **pend;
  5323.  
  5324.       rel_hdr = &elf_section_data (o)->rel_hdr;
  5325.  
  5326.       rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count;
  5327.  
  5328.       /* The contents field must last into write_object_contents,
  5329.          so we allocate it with bfd_alloc rather than malloc.  */
  5330.       rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size);
  5331.       if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
  5332.         {
  5333.           bfd_set_error (bfd_error_no_memory);
  5334.           goto error_return;
  5335.         }
  5336.  
  5337.       p = ((struct elf_link_hash_entry **)
  5338.            malloc (o->reloc_count
  5339.                * sizeof (struct elf_link_hash_entry *)));
  5340.       if (p == NULL && o->reloc_count != 0)
  5341.         {
  5342.           bfd_set_error (bfd_error_no_memory);
  5343.           goto error_return;
  5344.         }
  5345.       elf_section_data (o)->rel_hashes = p;
  5346.       pend = p + o->reloc_count;
  5347.       for (; p < pend; p++)
  5348.         *p = NULL;
  5349.  
  5350.       /* Use the reloc_count field as an index when outputting the
  5351.          relocs.  */
  5352.       o->reloc_count = 0;
  5353.     }
  5354.     }
  5355.  
  5356.   assign_file_positions_for_relocs (abfd);
  5357.  
  5358.   /* We have now assigned file positions for all the sections except
  5359.      .symtab and .strtab.  We start the .symtab section at the current
  5360.      file position, and write directly to it.  We build the .strtab
  5361.      section in memory.  When we add .dynsym support, we will build
  5362.      that in memory as well (.dynsym is smaller than .symtab).  */
  5363.   abfd->symcount = 0;
  5364.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  5365.   /* sh_name is set in prep_headers.  */
  5366.   symtab_hdr->sh_type = SHT_SYMTAB;
  5367.   symtab_hdr->sh_flags = 0;
  5368.   symtab_hdr->sh_addr = 0;
  5369.   symtab_hdr->sh_size = 0;
  5370.   symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
  5371.   /* sh_link is set in assign_section_numbers.  */
  5372.   /* sh_info is set below.  */
  5373.   /* sh_offset is set just below.  */
  5374.   symtab_hdr->sh_addralign = 4;  /* FIXME: system dependent?  */
  5375.  
  5376.   off = elf_tdata (abfd)->next_file_pos;
  5377.   off = assign_file_position_for_section (symtab_hdr, off, true);
  5378.  
  5379.   /* Note that at this point elf_tdata (abfd)->next_file_pos is
  5380.      incorrect.  We do not yet know the size of the .symtab section.
  5381.      We correct next_file_pos below, after we do know the size.  */
  5382.  
  5383.   /* Allocate a buffer to hold swapped out symbols.  This is to avoid
  5384.      continuously seeking to the right position in the file.  */
  5385.   if (! info->keep_memory || max_sym_count < 20)
  5386.     finfo.symbuf_size = 20;
  5387.   else
  5388.     finfo.symbuf_size = max_sym_count;
  5389.   finfo.symbuf = ((Elf_External_Sym *)
  5390.           malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
  5391.   if (finfo.symbuf == NULL)
  5392.     {
  5393.       bfd_set_error (bfd_error_no_memory);
  5394.       goto error_return;
  5395.     }
  5396.  
  5397.   /* Start writing out the symbol table.  The first symbol is always a
  5398.      dummy symbol.  */
  5399.   elfsym.st_value = 0;
  5400.   elfsym.st_size = 0;
  5401.   elfsym.st_info = 0;
  5402.   elfsym.st_other = 0;
  5403.   elfsym.st_shndx = SHN_UNDEF;
  5404.   if (! elf_link_output_sym (&finfo, (const char *) NULL,
  5405.                  &elfsym, bfd_und_section_ptr))
  5406.     goto error_return;
  5407.  
  5408. #if 0
  5409.   /* Some standard ELF linkers do this, but we don't because it causes
  5410.      bootstrap comparison failures.  */
  5411.   /* Output a file symbol for the output file as the second symbol.
  5412.      We output this even if we are discarding local symbols, although
  5413.      I'm not sure if this is correct.  */
  5414.   elfsym.st_value = 0;
  5415.   elfsym.st_size = 0;
  5416.   elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  5417.   elfsym.st_other = 0;
  5418.   elfsym.st_shndx = SHN_ABS;
  5419.   if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
  5420.                  &elfsym, bfd_abs_section_ptr))
  5421.     goto error_return;
  5422. #endif
  5423.  
  5424.   /* Output a symbol for each section.  We output these even if we are
  5425.      discarding local symbols, since they are used for relocs.  These
  5426.      symbols have no names.  We store the index of each one in the
  5427.      index field of the section, so that we can find it again when
  5428.      outputting relocs.  */
  5429.   elfsym.st_value = 0;
  5430.   elfsym.st_size = 0;
  5431.   elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  5432.   elfsym.st_other = 0;
  5433.   for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
  5434.     {
  5435.       o = section_from_elf_index (abfd, i);
  5436.       if (! bfd_is_abs_section (o))
  5437.     o->target_index = abfd->symcount;
  5438.       elfsym.st_shndx = i;
  5439.       if (! elf_link_output_sym (&finfo, (const char *) NULL,
  5440.                  &elfsym, o))
  5441.     goto error_return;
  5442.     }
  5443.  
  5444.   /* Allocate some memory to hold information read in from the input
  5445.      files.  */
  5446.   finfo.contents = (bfd_byte *) malloc (max_contents_size);
  5447.   finfo.external_relocs = (PTR) malloc (max_external_reloc_size);
  5448.   finfo.internal_relocs = ((Elf_Internal_Rela *)
  5449.                malloc (max_internal_reloc_count
  5450.                    * sizeof (Elf_Internal_Rela)));
  5451.   finfo.external_syms = ((Elf_External_Sym *)
  5452.              malloc (max_sym_count * sizeof (Elf_External_Sym)));
  5453.   finfo.internal_syms = ((Elf_Internal_Sym *)
  5454.              malloc (max_sym_count * sizeof (Elf_Internal_Sym)));
  5455.   finfo.indices = (long *) malloc (max_sym_count * sizeof (long));
  5456.   finfo.sections = (asection **) malloc (max_sym_count * sizeof (asection *));
  5457.   if ((finfo.contents == NULL && max_contents_size != 0)
  5458.       || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
  5459.       || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
  5460.       || (finfo.external_syms == NULL && max_sym_count != 0)
  5461.       || (finfo.internal_syms == NULL && max_sym_count != 0)
  5462.       || (finfo.indices == NULL && max_sym_count != 0)
  5463.       || (finfo.sections == NULL && max_sym_count != 0))
  5464.     {
  5465.       bfd_set_error (bfd_error_no_memory);
  5466.       goto error_return;
  5467.     }
  5468.  
  5469.   /* Since ELF permits relocations to be against local symbols, we
  5470.      must have the local symbols available when we do the relocations.
  5471.      Since we would rather only read the local symbols once, and we
  5472.      would rather not keep them in memory, we handle all the
  5473.      relocations for a single input file at the same time.
  5474.  
  5475.      Unfortunately, there is no way to know the total number of local
  5476.      symbols until we have seen all of them, and the local symbol
  5477.      indices precede the global symbol indices.  This means that when
  5478.      we are generating relocateable output, and we see a reloc against
  5479.      a global symbol, we can not know the symbol index until we have
  5480.      finished examining all the local symbols to see which ones we are
  5481.      going to output.  To deal with this, we keep the relocations in
  5482.      memory, and don't output them until the end of the link.  This is
  5483.      an unfortunate waste of memory, but I don't see a good way around
  5484.      it.  Fortunately, it only happens when performing a relocateable
  5485.      link, which is not the common case.  FIXME: If keep_memory is set
  5486.      we could write the relocs out and then read them again; I don't
  5487.      know how bad the memory loss will be.  */
  5488.  
  5489.   for (sub = info->input_bfds; sub != NULL; sub = sub->next)
  5490.     sub->output_has_begun = false;
  5491.   for (o = abfd->sections; o != NULL; o = o->next)
  5492.     {
  5493.       for (p = o->link_order_head; p != NULL; p = p->next)
  5494.     {
  5495.       if (p->type == bfd_indirect_link_order
  5496.           && (bfd_get_flavour (p->u.indirect.section->owner)
  5497.           == bfd_target_elf_flavour))
  5498.         {
  5499.           sub = p->u.indirect.section->owner;
  5500.           if (! sub->output_has_begun)
  5501.         {
  5502.           if (! elf_link_input_bfd (&finfo, sub))
  5503.             goto error_return;
  5504.           sub->output_has_begun = true;
  5505.         }
  5506.         }
  5507.       else if (p->type == bfd_section_reloc_link_order
  5508.            || p->type == bfd_symbol_reloc_link_order)
  5509.         {
  5510.           if (! elf_reloc_link_order (abfd, info, o, p))
  5511.         goto error_return;
  5512.         }
  5513.       else
  5514.         {
  5515.           if (! _bfd_default_link_order (abfd, info, o, p))
  5516.         goto error_return;
  5517.         }
  5518.     }
  5519.     }
  5520.  
  5521.   /* That wrote out all the local symbols.  Finish up the symbol table
  5522.      with the global symbols.  */
  5523.  
  5524.   /* The sh_info field records the index of the first non local
  5525.      symbol.  */
  5526.   symtab_hdr->sh_info = abfd->symcount;
  5527.   if (dynamic)
  5528.     elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1;
  5529.  
  5530.   /* We get the global symbols from the hash table.  */
  5531.   elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
  5532.               (PTR) &finfo);
  5533.  
  5534.   /* Flush all symbols to the file.  */
  5535.   if (! elf_link_flush_output_syms (&finfo))
  5536.     return false;
  5537.  
  5538.   /* Now we know the size of the symtab section.  */
  5539.   off += symtab_hdr->sh_size;
  5540.  
  5541.   /* Finish up the symbol string table (.strtab) section.  */
  5542.   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
  5543.   /* sh_name was set in prep_headers.  */
  5544.   symstrtab_hdr->sh_type = SHT_STRTAB;
  5545.   symstrtab_hdr->sh_flags = 0;
  5546.   symstrtab_hdr->sh_addr = 0;
  5547.   symstrtab_hdr->sh_size = finfo.symstrtab->length;
  5548.   symstrtab_hdr->sh_entsize = 0;
  5549.   symstrtab_hdr->sh_link = 0;
  5550.   symstrtab_hdr->sh_info = 0;
  5551.   /* sh_offset is set just below.  */
  5552.   symstrtab_hdr->sh_addralign = 1;
  5553.   symstrtab_hdr->contents = (PTR) finfo.symstrtab->tab;
  5554.  
  5555.   off = assign_file_position_for_section (symstrtab_hdr, off, true);
  5556.   elf_tdata (abfd)->next_file_pos = off;
  5557.  
  5558.   /* Adjust the relocs to have the correct symbol indices.  */
  5559.   for (o = abfd->sections; o != NULL; o = o->next)
  5560.     {
  5561.       struct elf_link_hash_entry **rel_hash;
  5562.       Elf_Internal_Shdr *rel_hdr;
  5563.  
  5564.       if ((o->flags & SEC_RELOC) == 0)
  5565.     continue;
  5566.  
  5567.       rel_hash = elf_section_data (o)->rel_hashes;
  5568.       rel_hdr = &elf_section_data (o)->rel_hdr;
  5569.       for (i = 0; i < o->reloc_count; i++, rel_hash++)
  5570.     {
  5571.       if (*rel_hash == NULL)
  5572.         continue;
  5573.           
  5574.       BFD_ASSERT ((*rel_hash)->indx >= 0);
  5575.  
  5576.       if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
  5577.         {
  5578.           Elf_External_Rel *erel;
  5579.           Elf_Internal_Rel irel;
  5580.  
  5581.           erel = (Elf_External_Rel *) rel_hdr->contents + i;
  5582.           elf_swap_reloc_in (abfd, erel, &irel);
  5583.           irel.r_info = ELF_R_INFO ((*rel_hash)->indx,
  5584.                     ELF_R_TYPE (irel.r_info));
  5585.           elf_swap_reloc_out (abfd, &irel, erel);
  5586.         }
  5587.       else
  5588.         {
  5589.           Elf_External_Rela *erela;
  5590.           Elf_Internal_Rela irela;
  5591.  
  5592.           BFD_ASSERT (rel_hdr->sh_entsize
  5593.               == sizeof (Elf_External_Rela));
  5594.  
  5595.           erela = (Elf_External_Rela *) rel_hdr->contents + i;
  5596.           elf_swap_reloca_in (abfd, erela, &irela);
  5597.           irela.r_info = ELF_R_INFO ((*rel_hash)->indx,
  5598.                      ELF_R_TYPE (irela.r_info));
  5599.           elf_swap_reloca_out (abfd, &irela, erela);
  5600.         }
  5601.     }
  5602.  
  5603.       /* Set the reloc_count field to 0 to prevent write_relocs from
  5604.      trying to swap the relocs out itself.  */
  5605.       o->reloc_count = 0;
  5606.     }
  5607.  
  5608.   /* If we are linking against a dynamic object, or generating a
  5609.      shared library, finish up the dynamic linking information.  */
  5610.   if (dynamic)
  5611.     {
  5612.       Elf_External_Dyn *dyncon, *dynconend;
  5613.  
  5614.       /* Fix up .dynamic entries.  */
  5615.       o = bfd_get_section_by_name (dynobj, ".dynamic");
  5616.       BFD_ASSERT (o != NULL);
  5617.  
  5618.       dyncon = (Elf_External_Dyn *) o->contents;
  5619.       dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
  5620.       for (; dyncon < dynconend; dyncon++)
  5621.     {
  5622.       Elf_Internal_Dyn dyn;
  5623.       const char *name;
  5624.       unsigned int type;
  5625.  
  5626.       elf_swap_dyn_in (dynobj, dyncon, &dyn);
  5627.  
  5628.       switch (dyn.d_tag)
  5629.         {
  5630.         default:
  5631.           break;
  5632.  
  5633.         case DT_INIT:
  5634.           name = ".init";
  5635.           goto get_vma;
  5636.         case DT_FINI:
  5637.           name = ".fini";
  5638.           goto get_vma;
  5639.         case DT_HASH:
  5640.           name = ".hash";
  5641.           goto get_vma;
  5642.         case DT_STRTAB:
  5643.           name = ".dynstr";
  5644.           goto get_vma;
  5645.         case DT_SYMTAB:
  5646.           name = ".dynsym";
  5647.         get_vma:
  5648.           o = bfd_get_section_by_name (abfd, name);
  5649.           BFD_ASSERT (o != NULL);
  5650.           dyn.d_un.d_ptr = o->vma;
  5651.           elf_swap_dyn_out (dynobj, &dyn, dyncon);
  5652.           break;
  5653.  
  5654.         case DT_REL:
  5655.         case DT_RELA:
  5656.         case DT_RELSZ:
  5657.         case DT_RELASZ:
  5658.           if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
  5659.         type = SHT_REL;
  5660.           else
  5661.         type = SHT_RELA;
  5662.           dyn.d_un.d_val = 0;
  5663.           for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
  5664.         {
  5665.           Elf_Internal_Shdr *hdr;
  5666.  
  5667.           hdr = elf_elfsections (abfd)[i];
  5668.           if (hdr->sh_type == type
  5669.               && (hdr->sh_flags & SHF_ALLOC) != 0)
  5670.             {
  5671.               if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
  5672.             dyn.d_un.d_val += hdr->sh_size;
  5673.               else
  5674.             {
  5675.               if (dyn.d_un.d_val == 0
  5676.                   || hdr->sh_addr < dyn.d_un.d_val)
  5677.                 dyn.d_un.d_val = hdr->sh_addr;
  5678.             }
  5679.             }
  5680.         }
  5681.           elf_swap_dyn_out (dynobj, &dyn, dyncon);
  5682.           break;
  5683.         }
  5684.     }
  5685.     }
  5686.  
  5687.   /* If we have created any dynamic sections, then output them.  */
  5688.   if (dynobj != NULL)
  5689.     {
  5690.       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
  5691.     goto error_return;
  5692.  
  5693.       for (o = dynobj->sections; o != NULL; o = o->next)
  5694.     {
  5695.       if ((o->flags & SEC_HAS_CONTENTS) == 0
  5696.           || o->_raw_size == 0)
  5697.         continue;
  5698.       if ((o->flags & SEC_IN_MEMORY) == 0)
  5699.         {
  5700.           /* At this point, we are only interested in sections
  5701.                  created by elf_link_create_dynamic_sections.  FIXME:
  5702.                  This test is fragile.  */
  5703.           continue;
  5704.         }
  5705.       if (! bfd_set_section_contents (abfd, o->output_section,
  5706.                       o->contents, o->output_offset,
  5707.                       o->_raw_size))
  5708.         goto error_return;
  5709.     }
  5710.     }
  5711.  
  5712.   if (finfo.contents != NULL)
  5713.     free (finfo.contents);
  5714.   if (finfo.external_relocs != NULL)
  5715.     free (finfo.external_relocs);
  5716.   if (finfo.internal_relocs != NULL)
  5717.     free (finfo.internal_relocs);
  5718.   if (finfo.external_syms != NULL)
  5719.     free (finfo.external_syms);
  5720.   if (finfo.internal_syms != NULL)
  5721.     free (finfo.internal_syms);
  5722.   if (finfo.indices != NULL)
  5723.     free (finfo.indices);
  5724.   if (finfo.sections != NULL)
  5725.     free (finfo.sections);
  5726.   if (finfo.symbuf != NULL)
  5727.     free (finfo.symbuf);
  5728.   for (o = abfd->sections; o != NULL; o = o->next)
  5729.     {
  5730.       if ((o->flags & SEC_RELOC) != 0
  5731.       && elf_section_data (o)->rel_hashes != NULL)
  5732.     free (elf_section_data (o)->rel_hashes);
  5733.     }
  5734.  
  5735.   elf_tdata (abfd)->linker = true;
  5736.  
  5737.   return true;
  5738.  
  5739.  error_return:
  5740.   if (finfo.contents != NULL)
  5741.     free (finfo.contents);
  5742.   if (finfo.external_relocs != NULL)
  5743.     free (finfo.external_relocs);
  5744.   if (finfo.internal_relocs != NULL)
  5745.     free (finfo.internal_relocs);
  5746.   if (finfo.external_syms != NULL)
  5747.     free (finfo.external_syms);
  5748.   if (finfo.internal_syms != NULL)
  5749.     free (finfo.internal_syms);
  5750.   if (finfo.indices != NULL)
  5751.     free (finfo.indices);
  5752.   if (finfo.sections != NULL)
  5753.     free (finfo.sections);
  5754.   if (finfo.symbuf != NULL)
  5755.     free (finfo.symbuf);
  5756.   for (o = abfd->sections; o != NULL; o = o->next)
  5757.     {
  5758.       if ((o->flags & SEC_RELOC) != 0
  5759.       && elf_section_data (o)->rel_hashes != NULL)
  5760.     free (elf_section_data (o)->rel_hashes);
  5761.     }
  5762.  
  5763.   return false;
  5764. }
  5765.  
  5766. /* Add a symbol to the output symbol table.  */
  5767.  
  5768. static boolean
  5769. elf_link_output_sym (finfo, name, elfsym, input_sec)
  5770.      struct elf_final_link_info *finfo;
  5771.      const char *name;
  5772.      Elf_Internal_Sym *elfsym;
  5773.      asection *input_sec;
  5774. {
  5775.   boolean (*output_symbol_hook) PARAMS ((bfd *,
  5776.                      struct bfd_link_info *info,
  5777.                      const char *,
  5778.                      Elf_Internal_Sym *,
  5779.                      asection *));
  5780.  
  5781.   output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
  5782.     elf_backend_link_output_symbol_hook;
  5783.   if (output_symbol_hook != NULL)
  5784.     {
  5785.       if (! ((*output_symbol_hook)
  5786.          (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
  5787.     return false;
  5788.     }
  5789.  
  5790.   if (name == (const char *) NULL || *name == '\0')
  5791.     elfsym->st_name = 0;
  5792.   else
  5793.     {
  5794.       elfsym->st_name = bfd_add_to_strtab (finfo->output_bfd,
  5795.                        finfo->symstrtab, name);
  5796.       if (elfsym->st_name == (unsigned long) -1)
  5797.     return false;
  5798.     }
  5799.  
  5800.   if (finfo->symbuf_count >= finfo->symbuf_size)
  5801.     {
  5802.       if (! elf_link_flush_output_syms (finfo))
  5803.     return false;
  5804.     }
  5805.  
  5806.   elf_swap_symbol_out (finfo->output_bfd, elfsym,
  5807.                finfo->symbuf + finfo->symbuf_count);
  5808.   ++finfo->symbuf_count;
  5809.  
  5810.   ++finfo->output_bfd->symcount;
  5811.  
  5812.   return true;
  5813. }
  5814.  
  5815. /* Flush the output symbols to the file.  */
  5816.  
  5817. static boolean
  5818. elf_link_flush_output_syms (finfo)
  5819.      struct elf_final_link_info *finfo;
  5820. {
  5821.   Elf_Internal_Shdr *symtab;
  5822.  
  5823.   symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
  5824.  
  5825.   if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
  5826.         SEEK_SET) != 0
  5827.       || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
  5828.              sizeof (Elf_External_Sym), finfo->output_bfd)
  5829.       != finfo->symbuf_count * sizeof (Elf_External_Sym)))
  5830.     return false;
  5831.  
  5832.   symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
  5833.  
  5834.   finfo->symbuf_count = 0;
  5835.  
  5836.   return true;
  5837. }
  5838.  
  5839. /* Add an external symbol to the symbol table.  This is called from
  5840.    the hash table traversal routine.  */
  5841.  
  5842. static boolean
  5843. elf_link_output_extsym (h, data)
  5844.      struct elf_link_hash_entry *h;
  5845.      PTR data;
  5846. {
  5847.   struct elf_final_link_info *finfo = (struct elf_final_link_info *) data;
  5848.   boolean strip;
  5849.   Elf_Internal_Sym sym;
  5850.   asection *input_sec;
  5851.  
  5852.   /* We don't want to output symbols that have never been mentioned by
  5853.      a regular file, or that we have been told to strip.  However, if
  5854.      h->indx is set to -2, the symbol is used by a reloc and we must
  5855.      output it.  */
  5856.   if (h->indx == -2)
  5857.     strip = false;
  5858.   else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  5859.         || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
  5860.        && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
  5861.        && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
  5862.     strip = true;
  5863.   else if (finfo->info->strip == strip_all
  5864.        || (finfo->info->strip == strip_some
  5865.            && bfd_hash_lookup (finfo->info->keep_hash,
  5866.                    h->root.root.string,
  5867.                    false, false) == NULL))
  5868.     strip = true;
  5869.   else
  5870.     strip = false;
  5871.  
  5872.   /* If we're stripping it, and it's not a dynamic symbol, there's
  5873.      nothing else to do.  */
  5874.   if (strip && h->dynindx == -1)
  5875.     return true;
  5876.  
  5877.   sym.st_value = 0;
  5878.   sym.st_size = h->size;
  5879.   sym.st_other = 0;
  5880.   if (h->root.type == bfd_link_hash_weak
  5881.       || (h->elf_link_hash_flags & ELF_LINK_HASH_DEFINED_WEAK) != 0)
  5882.     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
  5883.   else
  5884.     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
  5885.  
  5886.   switch (h->root.type)
  5887.     {
  5888.     default:
  5889.     case bfd_link_hash_new:
  5890.       abort ();
  5891.       return false;
  5892.  
  5893.     case bfd_link_hash_undefined:
  5894.       input_sec = bfd_und_section_ptr;
  5895.       sym.st_shndx = SHN_UNDEF;
  5896.       break;
  5897.  
  5898.     case bfd_link_hash_weak:
  5899.       input_sec = bfd_und_section_ptr;
  5900.       sym.st_shndx = SHN_UNDEF;
  5901.       break;
  5902.  
  5903.     case bfd_link_hash_defined:
  5904.       {
  5905.  
  5906.     input_sec = h->root.u.def.section;
  5907.     if (input_sec->output_section != NULL)
  5908.       {
  5909.         sym.st_shndx = elf_section_from_bfd_section (finfo->output_bfd,
  5910.                              input_sec->output_section);
  5911.         if (sym.st_shndx == (unsigned short) -1)
  5912.           {
  5913.         /* FIXME: No way to handle errors.  */
  5914.         abort ();
  5915.           }
  5916.  
  5917.         /* ELF symbols in relocateable files are section relative,
  5918.            but in nonrelocateable files they are virtual
  5919.            addresses.  */
  5920.         sym.st_value = h->root.u.def.value + input_sec->output_offset;
  5921.         if (! finfo->info->relocateable)
  5922.           sym.st_value += input_sec->output_section->vma;
  5923.       }
  5924.     else
  5925.       {
  5926.         BFD_ASSERT (bfd_get_flavour (input_sec->owner)
  5927.             == bfd_target_elf_flavour
  5928.             && elf_elfheader (input_sec->owner)->e_type == ET_DYN);
  5929.         sym.st_shndx = SHN_UNDEF;
  5930.         input_sec = bfd_und_section_ptr;
  5931.       }
  5932.       }
  5933.       break;
  5934.  
  5935.     case bfd_link_hash_common:
  5936.       input_sec = bfd_com_section_ptr;
  5937.       sym.st_shndx = SHN_COMMON;
  5938.       sym.st_value = 1 << h->root.u.c.alignment_power;
  5939.       break;
  5940.  
  5941.     case bfd_link_hash_indirect:
  5942.     case bfd_link_hash_warning:
  5943.       /* I have no idea how these should be handled.  */
  5944.       return true;
  5945.     }
  5946.  
  5947.   /* If this symbol should be put in the .dynsym section, then put it
  5948.      there now.  We have already know the symbol index.  We also fill
  5949.      in the entry in the .hash section.  */
  5950.   if (h->dynindx != -1
  5951.       && elf_hash_table (finfo->info)->dynamic_sections_created)
  5952.     {
  5953.       struct elf_backend_data *bed;
  5954.       size_t bucketcount;
  5955.       size_t bucket;
  5956.       bfd_byte *bucketpos;
  5957.       bfd_vma chain;
  5958.  
  5959.       sym.st_name = h->dynstr_index;
  5960.  
  5961.       /* Give the processor backend a chance to tweak the symbol
  5962.      value, and also to finish up anything that needs to be done
  5963.      for this symbol.  */
  5964.       bed = get_elf_backend_data (finfo->output_bfd);
  5965.       if (! ((*bed->elf_backend_finish_dynamic_symbol)
  5966.          (finfo->output_bfd, finfo->info, h, &sym)))
  5967.     {
  5968.       /* FIXME: No way to return error.  */
  5969.       abort ();
  5970.     }
  5971.  
  5972.       elf_swap_symbol_out (finfo->output_bfd, &sym,
  5973.                ((Elf_External_Sym *) finfo->dynsym_sec->contents
  5974.                 + h->dynindx));
  5975.  
  5976.       bucketcount = elf_hash_table (finfo->info)->bucketcount;
  5977.       bucket = (bfd_elf_hash ((const unsigned char *) h->root.root.string)
  5978.         % bucketcount);
  5979.       bucketpos = ((bfd_byte *) finfo->hash_sec->contents
  5980.            + (bucket + 2) * (ARCH_SIZE / 8));
  5981.       chain = get_word (finfo->output_bfd, bucketpos);
  5982.       put_word (finfo->output_bfd, h->dynindx, bucketpos);
  5983.       put_word (finfo->output_bfd, chain,
  5984.         ((bfd_byte *) finfo->hash_sec->contents
  5985.          + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8)));
  5986.     }
  5987.  
  5988.   /* If we're stripping it, then it was just a dynamic symbol, and
  5989.      there's nothing else to do.  */
  5990.   if (strip)
  5991.     return true;
  5992.  
  5993.   h->indx = finfo->output_bfd->symcount;
  5994.  
  5995.   if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
  5996.     {
  5997.       /* FIXME: No way to return error.  */
  5998.       abort ();
  5999.     }
  6000.  
  6001.   return true;
  6002. }
  6003.  
  6004. /* Link an input file into the linker output file.  This function
  6005.    handles all the sections and relocations of the input file at once.
  6006.    This is so that we only have to read the local symbols once, and
  6007.    don't have to keep them in memory.  */
  6008.  
  6009. static boolean
  6010. elf_link_input_bfd (finfo, input_bfd)
  6011.      struct elf_final_link_info *finfo;
  6012.      bfd *input_bfd;
  6013. {
  6014.   boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
  6015.                        bfd *, asection *, bfd_byte *,
  6016.                        Elf_Internal_Rela *,
  6017.                        Elf_Internal_Sym *,
  6018.                        asection **, char *));
  6019.   bfd *output_bfd;
  6020.   Elf_Internal_Shdr *symtab_hdr;
  6021.   size_t locsymcount;
  6022.   size_t extsymoff;
  6023.   Elf_External_Sym *esym;
  6024.   Elf_External_Sym *esymend;
  6025.   Elf_Internal_Sym *isym;
  6026.   long *pindex;
  6027.   asection **ppsection;
  6028.   asection *o;
  6029.  
  6030.   output_bfd = finfo->output_bfd;
  6031.   relocate_section =
  6032.     get_elf_backend_data (output_bfd)->elf_backend_relocate_section;
  6033.  
  6034.   /* If this is a dynamic object, we don't want to do anything here:
  6035.      we don't want the local symbols, and we don't want the section
  6036.      contents.  */
  6037.   if (elf_elfheader (input_bfd)->e_type == ET_DYN)
  6038.     return true;
  6039.  
  6040.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  6041.   if (elf_bad_symtab (input_bfd))
  6042.     {
  6043.       locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
  6044.       extsymoff = 0;
  6045.     }
  6046.   else
  6047.     {
  6048.       locsymcount = symtab_hdr->sh_info;
  6049.       extsymoff = symtab_hdr->sh_info;
  6050.     }
  6051.  
  6052.   /* Read the local symbols.  */
  6053.   if (locsymcount > 0
  6054.       && (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
  6055.             || (bfd_read (finfo->external_syms, sizeof (Elf_External_Sym),
  6056.             locsymcount, input_bfd)
  6057.           != locsymcount * sizeof (Elf_External_Sym))))
  6058.     return false;
  6059.  
  6060.   /* Swap in the local symbols and write out the ones which we know
  6061.      are going into the output file.  */
  6062.   esym = finfo->external_syms;
  6063.   esymend = esym + locsymcount;
  6064.   isym = finfo->internal_syms;
  6065.   pindex = finfo->indices;
  6066.   ppsection = finfo->sections;
  6067.   for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
  6068.     {
  6069.       asection *isec;
  6070.       const char *name;
  6071.       bfd_vma oldval;
  6072.  
  6073.       elf_swap_symbol_in (input_bfd, esym, isym);
  6074.       *pindex = -1;
  6075.  
  6076.       if (elf_bad_symtab (input_bfd))
  6077.     {
  6078.       if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
  6079.         {
  6080.           *ppsection = NULL;
  6081.           continue;
  6082.         }
  6083.     }
  6084.  
  6085.       if (isym->st_shndx == SHN_UNDEF)
  6086.     isec = bfd_und_section_ptr;
  6087.       else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
  6088.     {
  6089.       isec = section_from_elf_index (input_bfd, isym->st_shndx);
  6090.       if (isec == NULL)
  6091.         return false;
  6092.     }
  6093.       else if (isym->st_shndx == SHN_ABS)
  6094.     isec = bfd_abs_section_ptr;
  6095.       else if (isym->st_shndx == SHN_COMMON)
  6096.     isec = bfd_com_section_ptr;
  6097.       else
  6098.     {
  6099.       /* Who knows?  */
  6100.       isec = NULL;
  6101.     }
  6102.  
  6103.       *ppsection = isec;
  6104.  
  6105.       /* Don't output the first, undefined, symbol.  */
  6106.       if (esym == finfo->external_syms)
  6107.     continue;
  6108.  
  6109.       /* If we are stripping all symbols, we don't want to output this
  6110.      one.  */
  6111.       if (finfo->info->strip == strip_all)
  6112.     continue;
  6113.  
  6114.       /* We never output section symbols.  Instead, we use the section
  6115.      symbol of the corresponding section in the output file.  */
  6116.       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  6117.     continue;
  6118.  
  6119.       /* If we are discarding all local symbols, we don't want to
  6120.      output this one.  If we are generating a relocateable output
  6121.      file, then some of the local symbols may be required by
  6122.      relocs; we output them below as we discover that they are
  6123.      needed.  */
  6124.       if (finfo->info->discard == discard_all)
  6125.     continue;
  6126.  
  6127.       /* Get the name of the symbol.  */
  6128.       name = elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
  6129.                       isym->st_name);
  6130.       if (name == NULL)
  6131.     return false;
  6132.  
  6133.       /* See if we are discarding symbols with this name.  */
  6134.       if ((finfo->info->strip == strip_some
  6135.        && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
  6136.            == NULL))
  6137.       || (finfo->info->discard == discard_l
  6138.           && strncmp (name, finfo->info->lprefix,
  6139.               finfo->info->lprefix_len) == 0))
  6140.     continue;
  6141.  
  6142.       /* If we get here, we are going to output this symbol.  */
  6143.  
  6144.       /* Adjust the section index for the output file.  */
  6145.       isym->st_shndx = elf_section_from_bfd_section (output_bfd,
  6146.                              isec->output_section);
  6147.       if (isym->st_shndx == (unsigned short) -1)
  6148.     return false;
  6149.  
  6150.       *pindex = output_bfd->symcount;
  6151.  
  6152.       /* ELF symbols in relocateable files are section relative, but
  6153.      in executable files they are virtual addresses.  Note that
  6154.      this code assumes that all ELF sections have an associated
  6155.      BFD section with a reasonable value for output_offset; below
  6156.      we assume that they also have a reasonable value for
  6157.      output_section.  Any special sections must be set up to meet
  6158.      these requirements.  */
  6159.       oldval = isym->st_value;
  6160.       isym->st_value += isec->output_offset;
  6161.       if (! finfo->info->relocateable)
  6162.     isym->st_value += isec->output_section->vma;
  6163.  
  6164.       if (! elf_link_output_sym (finfo, name, isym, isec))
  6165.     return false;
  6166.  
  6167.       /* Restore the old value for reloc handling.  */
  6168.       isym->st_value = oldval;
  6169.     }
  6170.  
  6171.   /* Relocate the contents of each section.  */
  6172.   for (o = input_bfd->sections; o != NULL; o = o->next)
  6173.     {
  6174.       if ((o->flags & SEC_HAS_CONTENTS) == 0)
  6175.     continue;
  6176.  
  6177.       if ((o->flags & SEC_IN_MEMORY) != 0
  6178.       && input_bfd == elf_hash_table (finfo->info)->dynobj)
  6179.     {
  6180.       /* Section was created by elf_link_create_dynamic_sections.
  6181.              FIXME: This test is fragile.  */
  6182.       continue;
  6183.     }
  6184.  
  6185.       /* Read the contents of the section.  */
  6186.       if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
  6187.                       (file_ptr) 0, o->_raw_size))
  6188.     return false;
  6189.  
  6190.       if ((o->flags & SEC_RELOC) != 0)
  6191.     {
  6192.       Elf_Internal_Rela *internal_relocs;
  6193.  
  6194.       /* Get the swapped relocs.  */
  6195.       internal_relocs = elf_link_read_relocs (input_bfd, o,
  6196.                           finfo->external_relocs,
  6197.                           finfo->internal_relocs,
  6198.                           false);
  6199.       if (internal_relocs == NULL
  6200.           && o->reloc_count > 0)
  6201.         return false;
  6202.  
  6203.       /* Relocate the section by invoking a back end routine.
  6204.  
  6205.          The back end routine is responsible for adjusting the
  6206.          section contents as necessary, and (if using Rela relocs
  6207.          and generating a relocateable output file) adjusting the
  6208.          reloc addend as necessary.
  6209.  
  6210.          The back end routine does not have to worry about setting
  6211.          the reloc address or the reloc symbol index.
  6212.  
  6213.          The back end routine is given a pointer to the swapped in
  6214.          internal symbols, and can access the hash table entries
  6215.          for the external symbols via elf_sym_hashes (input_bfd).
  6216.  
  6217.          When generating relocateable output, the back end routine
  6218.          must handle STB_LOCAL/STT_SECTION symbols specially.  The
  6219.          output symbol is going to be a section symbol
  6220.          corresponding to the output section, which will require
  6221.          the addend to be adjusted.  */
  6222.  
  6223.       if (! (*relocate_section) (output_bfd, finfo->info,
  6224.                      input_bfd, o,
  6225.                      finfo->contents,
  6226.                      internal_relocs,
  6227.                      finfo->internal_syms,
  6228.                      finfo->sections,
  6229.                      finfo->symstrtab->tab))
  6230.         return false;
  6231.  
  6232.       if (finfo->info->relocateable)
  6233.         {
  6234.           Elf_Internal_Rela *irela;
  6235.           Elf_Internal_Rela *irelaend;
  6236.           struct elf_link_hash_entry **rel_hash;
  6237.           Elf_Internal_Shdr *input_rel_hdr;
  6238.           Elf_Internal_Shdr *output_rel_hdr;
  6239.  
  6240.           /* Adjust the reloc addresses and symbol indices.  */
  6241.  
  6242.           irela = internal_relocs;
  6243.           irelaend = irela + o->reloc_count;
  6244.           rel_hash = (elf_section_data (o->output_section)->rel_hashes
  6245.               + o->output_section->reloc_count);
  6246.           for (; irela < irelaend; irela++, rel_hash++)
  6247.         {
  6248.           long r_symndx;
  6249.           Elf_Internal_Sym *isym;
  6250.           asection *sec;
  6251.  
  6252.           irela->r_offset += o->output_offset;
  6253.  
  6254.           r_symndx = ELF_R_SYM (irela->r_info);
  6255.  
  6256.           if (r_symndx == 0)
  6257.             continue;
  6258.  
  6259.           if (r_symndx >= locsymcount
  6260.               || (elf_bad_symtab (input_bfd)
  6261.               && finfo->sections[r_symndx] == NULL))
  6262.             {
  6263.               long indx;
  6264.  
  6265.               /* This is a reloc against a global symbol.  We
  6266.              have not yet output all the local symbols, so
  6267.              we do not know the symbol index of any global
  6268.              symbol.  We set the rel_hash entry for this
  6269.              reloc to point to the global hash table entry
  6270.              for this symbol.  The symbol index is then
  6271.              set at the end of elf_bfd_final_link.  */
  6272.               indx = r_symndx - extsymoff;
  6273.               *rel_hash = elf_sym_hashes (input_bfd)[indx];
  6274.  
  6275.               /* Setting the index to -2 tells
  6276.              elf_link_output_extsym that this symbol is
  6277.              used by a reloc.  */
  6278.               BFD_ASSERT ((*rel_hash)->indx < 0);
  6279.               (*rel_hash)->indx = -2;
  6280.  
  6281.               continue;
  6282.             }
  6283.  
  6284.           /* This is a reloc against a local symbol. */
  6285.  
  6286.           *rel_hash = NULL;
  6287.           isym = finfo->internal_syms + r_symndx;
  6288.           sec = finfo->sections[r_symndx];
  6289.           if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  6290.             {
  6291.               /* I suppose the backend ought to fill in the
  6292.              section of any STT_SECTION symbol against a
  6293.              processor specific section.  */
  6294.               if (sec != NULL && bfd_is_abs_section (sec))
  6295.             r_symndx = 0;
  6296.               else if (sec == NULL || sec->owner == NULL)
  6297.             {
  6298.               bfd_set_error (bfd_error_bad_value);
  6299.               return false;
  6300.             }
  6301.               else
  6302.             {
  6303.               r_symndx = sec->output_section->target_index;
  6304.               if (r_symndx == 0)
  6305.                 abort ();
  6306.             }
  6307.             }
  6308.           else
  6309.             {
  6310.               if (finfo->indices[r_symndx] == -1)
  6311.             {
  6312.               unsigned long link;
  6313.               const char *name;
  6314.               asection *osec;
  6315.  
  6316.               if (finfo->info->strip == strip_all)
  6317.                 {
  6318.                   /* You can't do ld -r -s.  */
  6319.                   bfd_set_error (bfd_error_invalid_operation);
  6320.                   return false;
  6321.                 }
  6322.  
  6323.               /* This symbol was skipped earlier, but
  6324.                  since it is needed by a reloc, we
  6325.                  must output it now.  */
  6326.               link = symtab_hdr->sh_link;
  6327.               name = elf_string_from_elf_section (input_bfd,
  6328.                                   link,
  6329.                                   isym->st_name);
  6330.               if (name == NULL)
  6331.                 return false;
  6332.  
  6333.               osec = sec->output_section;
  6334.               isym->st_shndx =
  6335.                 elf_section_from_bfd_section (output_bfd,
  6336.                               osec);
  6337.               if (isym->st_shndx == (unsigned short) -1)
  6338.                 return false;
  6339.  
  6340.               isym->st_value += sec->output_offset;
  6341.               if (! finfo->info->relocateable)
  6342.                 isym->st_value += osec->vma;
  6343.  
  6344.               finfo->indices[r_symndx] = output_bfd->symcount;
  6345.  
  6346.               if (! elf_link_output_sym (finfo, name, isym, sec))
  6347.                 return false;
  6348.             }
  6349.  
  6350.               r_symndx = finfo->indices[r_symndx];
  6351.             }
  6352.  
  6353.           irela->r_info = ELF_R_INFO (r_symndx,
  6354.                           ELF_R_TYPE (irela->r_info));
  6355.         }
  6356.  
  6357.           /* Swap out the relocs.  */
  6358.           input_rel_hdr = &elf_section_data (o)->rel_hdr;
  6359.           output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr;
  6360.           BFD_ASSERT (output_rel_hdr->sh_entsize
  6361.               == input_rel_hdr->sh_entsize);
  6362.           irela = internal_relocs;
  6363.           irelaend = irela + o->reloc_count;
  6364.           if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
  6365.         {
  6366.           Elf_External_Rel *erel;
  6367.  
  6368.           erel = ((Elf_External_Rel *) output_rel_hdr->contents
  6369.               + o->output_section->reloc_count);
  6370.           for (; irela < irelaend; irela++, erel++)
  6371.             {
  6372.               Elf_Internal_Rel irel;
  6373.  
  6374.               irel.r_offset = irela->r_offset;
  6375.               irel.r_info = irela->r_info;
  6376.               BFD_ASSERT (irela->r_addend == 0);
  6377.               elf_swap_reloc_out (output_bfd, &irel, erel);
  6378.             }
  6379.         }
  6380.           else
  6381.         {
  6382.           Elf_External_Rela *erela;
  6383.  
  6384.           BFD_ASSERT (input_rel_hdr->sh_entsize
  6385.                   == sizeof (Elf_External_Rela));
  6386.           erela = ((Elf_External_Rela *) output_rel_hdr->contents
  6387.                + o->output_section->reloc_count);
  6388.           for (; irela < irelaend; irela++, erela++)
  6389.             elf_swap_reloca_out (output_bfd, irela, erela);
  6390.         }
  6391.  
  6392.           o->output_section->reloc_count += o->reloc_count;
  6393.         }
  6394.     }
  6395.  
  6396.       /* Write out the modified section contents.  */
  6397.       if (! bfd_set_section_contents (output_bfd, o->output_section,
  6398.                       finfo->contents, o->output_offset,
  6399.                       (o->_cooked_size != 0
  6400.                        ? o->_cooked_size
  6401.                        : o->_raw_size)))
  6402.     return false;
  6403.     }
  6404.  
  6405.   return true;
  6406. }
  6407.  
  6408. /* Generate a reloc when linking an ELF file.  This is a reloc
  6409.    requested by the linker, and does come from any input file.  This
  6410.    is used to build constructor and destructor tables when linking
  6411.    with -Ur.  */
  6412.  
  6413. static boolean
  6414. elf_reloc_link_order (output_bfd, info, output_section, link_order)
  6415.      bfd *output_bfd;
  6416.      struct bfd_link_info *info;
  6417.      asection *output_section;
  6418.      struct bfd_link_order *link_order;
  6419. {
  6420.   const reloc_howto_type *howto;
  6421.   long indx;
  6422.   bfd_vma offset;
  6423.   struct elf_link_hash_entry **rel_hash_ptr;
  6424.   Elf_Internal_Shdr *rel_hdr;
  6425.  
  6426.   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
  6427.   if (howto == NULL)
  6428.     {
  6429.       bfd_set_error (bfd_error_bad_value);
  6430.       return false;
  6431.     }
  6432.  
  6433.   /* If this is an inplace reloc, we must write the addend into the
  6434.      object file.  */
  6435.   if (howto->partial_inplace
  6436.       && link_order->u.reloc.p->addend != 0)
  6437.     {
  6438.       bfd_size_type size;
  6439.       bfd_reloc_status_type rstat;
  6440.       bfd_byte *buf;
  6441.       boolean ok;
  6442.  
  6443.       size = bfd_get_reloc_size (howto);
  6444.       buf = (bfd_byte *) bfd_zmalloc (size);
  6445.       if (buf == (bfd_byte *) NULL)
  6446.     {
  6447.       bfd_set_error (bfd_error_no_memory);
  6448.       return false;
  6449.     }
  6450.       rstat = _bfd_relocate_contents (howto, output_bfd,
  6451.                       link_order->u.reloc.p->addend, buf);
  6452.       switch (rstat)
  6453.     {
  6454.     case bfd_reloc_ok:
  6455.       break;
  6456.     default:
  6457.     case bfd_reloc_outofrange:
  6458.       abort ();
  6459.     case bfd_reloc_overflow:
  6460.       if (! ((*info->callbacks->reloc_overflow)
  6461.          (info,
  6462.           (link_order->type == bfd_section_reloc_link_order
  6463.            ? bfd_section_name (output_bfd,
  6464.                        link_order->u.reloc.p->u.section)
  6465.            : link_order->u.reloc.p->u.name),
  6466.           howto->name, link_order->u.reloc.p->addend,
  6467.           (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
  6468.         {
  6469.           free (buf);
  6470.           return false;
  6471.         }
  6472.       break;
  6473.     }
  6474.       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
  6475.                      (file_ptr) link_order->offset, size);
  6476.       free (buf);
  6477.       if (! ok)
  6478.     return false;
  6479.     }
  6480.  
  6481.   /* Figure out the symbol index.  */
  6482.   rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
  6483.           + output_section->reloc_count);
  6484.   if (link_order->type == bfd_section_reloc_link_order)
  6485.     {
  6486.       indx = link_order->u.reloc.p->u.section->target_index;
  6487.       if (indx == 0)
  6488.     abort ();
  6489.       *rel_hash_ptr = NULL;
  6490.     }
  6491.   else
  6492.     {
  6493.       struct elf_link_hash_entry *h;
  6494.  
  6495.       h = elf_link_hash_lookup (elf_hash_table (info),
  6496.                 link_order->u.reloc.p->u.name,
  6497.                 false, false, true);
  6498.       if (h != NULL)
  6499.     {
  6500.       /* Setting the index to -2 tells elf_link_output_extsym that
  6501.          this symbol is used by a reloc.  */
  6502.       h->indx = -2;
  6503.       *rel_hash_ptr = h;
  6504.       indx = 0;
  6505.     }
  6506.       else
  6507.     {
  6508.       if (! ((*info->callbacks->unattached_reloc)
  6509.          (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
  6510.           (asection *) NULL, (bfd_vma) 0)))
  6511.         return false;
  6512.       indx = 0;
  6513.     }
  6514.     }
  6515.  
  6516.   /* The address of a reloc is relative to the section in a
  6517.      relocateable file, and is a virtual address in an executable
  6518.      file.  */
  6519.   offset = link_order->offset;
  6520.   if (! info->relocateable)
  6521.     offset += output_section->vma;
  6522.  
  6523.   rel_hdr = &elf_section_data (output_section)->rel_hdr;
  6524.  
  6525.   if (rel_hdr->sh_type == SHT_REL)
  6526.     {
  6527.       Elf_Internal_Rel irel;
  6528.       Elf_External_Rel *erel;
  6529.  
  6530.       irel.r_offset = offset;
  6531.       irel.r_info = ELF_R_INFO (indx, howto->type);
  6532.       erel = ((Elf_External_Rel *) rel_hdr->contents
  6533.           + output_section->reloc_count);
  6534.       elf_swap_reloc_out (output_bfd, &irel, erel);
  6535.     }
  6536.   else
  6537.     {
  6538.       Elf_Internal_Rela irela;
  6539.       Elf_External_Rela *erela;
  6540.  
  6541.       irela.r_offset = offset;
  6542.       irela.r_info = ELF_R_INFO (indx, howto->type);
  6543.       irela.r_addend = link_order->u.reloc.p->addend;
  6544.       erela = ((Elf_External_Rela *) rel_hdr->contents
  6545.            + output_section->reloc_count);
  6546.       elf_swap_reloca_out (output_bfd, &irela, erela);
  6547.     }
  6548.  
  6549.   ++output_section->reloc_count;
  6550.  
  6551.   return true;
  6552. }
  6553.